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'; const here = dirname(fileURLToPath(import.meta.url)); const source = () => readFileSync(join(here, 'dist-umd', 'widget.js'), 'utf8'); test('同一份 UMD 在三种环境下都能导出', () => { // 1) 全局 const globalContext = vm.createContext({}); vm.runInContext(source(), globalContext); assert.equal(globalContext.Widget.version, '1.0.0'); // 2) AMD let amdFactory; const amdContext = vm.createContext({ define(deps, factory) { assert.deepEqual([...deps], []); amdFactory = factory; }, }); amdContext.define.amd = true; vm.runInContext(source(), amdContext); assert.equal(amdFactory().version, '1.0.0'); // 3) CJS const module = { exports: {} }; const cjsContext = vm.createContext({ module, exports: module.exports }); vm.runInContext(source(), cjsContext); assert.equal(module.exports.version, '1.0.0'); });