示例工程 14-runtime-syntax
14-runtime-syntax/02-namespace-output/main.test.mjs
点击「运行示例」后,左侧显示源码、右侧显示运行结果;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 { Geometry, run } from './dist/main.js';
const here = dirname(fileURLToPath(import.meta.url));
test('namespace 生成 IIFE,嵌套 namespace 是嵌套对象', () => {
const out = [];
const ret = run((msg) => out.push(msg));
assert.deepEqual(ret, ['3.14', 'circle', '3.14159']);
assert.deepEqual(out, ret);
assert.equal(Geometry.area(2).toFixed(2), '12.57');
const emitted = readFileSync(join(here, 'dist', 'main.js'), 'utf8');
assert.ok(emitted.includes('(function (Geometry)'), 'namespace 产物是 IIFE');
});