示例工程 18-iife-module-pattern
18-iife-module-pattern/03-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 { run } from './dist/main.js';
const here = dirname(fileURLToPath(import.meta.url));
test('namespace 产物是 IIFE;运行时提供对象', () => {
globalThis.LegacyUI = {
version: '1.0.0',
boot: () => 'boot:1.0.0',
Theme: { name: 'light' },
};
const out = [];
const ret = run((msg) => out.push(msg));
assert.deepEqual(ret, ['1.0.0', 'boot:1.0.0', 'light']);
assert.deepEqual(out, ret);
const emitted = readFileSync(join(here, 'dist-script', 'ui.js'), 'utf8');
assert.ok(emitted.includes('var LegacyUI'), 'namespace 产物声明对象');
assert.ok(emitted.includes('(function (LegacyUI)'), 'namespace 产物是 IIFE');
});