示例工程 25-declaration-distribution

25-declaration-distribution/01-generate-dts/main.test.mjs

javascript · 0.8 KB

运行:25 · generate-dts

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { run } from './dist/main.js';
 
const here = dirname(fileURLToPath(import.meta.url));
 
test('declaration emit 生成 .d.ts 与声明映射', () => {
  const out = [];
  const ret = run((msg) => out.push(msg));
  assert.deepEqual(ret, ['ADA']);
  assert.deepEqual(out, ret);
 
  const dts = readFileSync(join(here, 'dist', 'types', 'index.d.ts'), 'utf8');
  assert.ok(dts.includes('export interface User'), '接口被导出');
  assert.ok(dts.includes('export declare function displayName'), '函数声明被导出');
  assert.ok(!dts.includes('return user.name.toUpperCase'), '声明文件不含实现');
});