示例工程 19-namespaces-ts-modules
19-namespaces-ts-modules/02-namespace-split/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 vm from 'node:vm';
import { run } from './dist/main.js';
const here = dirname(fileURLToPath(import.meta.url));
test('两份脚本在共享上下文中合并成同一个 App', () => {
const context = vm.createContext({});
for (const file of ['app-core.js', 'app-extra.js']) {
const code = readFileSync(join(here, 'dist-script', file), 'utf8');
vm.runInContext(code, context);
}
assert.equal(context.App.version, '2.0.0');
assert.equal(context.App.boot(), 'boot:2.0.0');
globalThis.App = context.App;
const out = [];
const ret = run((msg) => out.push(msg));
assert.deepEqual(ret, ['2.0.0', 'boot:2.0.0']);
assert.deepEqual(out, ret);
});