测试侧用 vm.runInThisContext 加载浏览器全局脚本,正式代码不为测试加任何导出。 test_shared_sync 断言前后端 shared 逐字节一致,已做反向验证(改一个空格即红)。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
28 lines
864 B
JavaScript
28 lines
864 B
JavaScript
// 把前端 ES5 全局脚本加载进 Node global。
|
||
// 正式代码是浏览器脚本(var X = {...},无 module.exports),
|
||
// 这里用 vm.runInThisContext 还原浏览器语义——顶层 var 挂到 globalThis。
|
||
// 【关键】不得为了测试而给正式代码加 module.exports(client 05 §10)。
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const vm = require('vm');
|
||
|
||
const ROOT = path.resolve(__dirname, '..', '..'); // 仓库根
|
||
|
||
function load(rel) {
|
||
const abs = path.join(ROOT, rel);
|
||
const code = fs.readFileSync(abs, 'utf8');
|
||
vm.runInThisContext(code, { filename: rel });
|
||
}
|
||
|
||
function loadAll(rels) {
|
||
rels.forEach(load);
|
||
}
|
||
|
||
// 断言抛错的小工具:期望 fn 抛异常
|
||
function throws(fn) {
|
||
try { fn(); } catch (e) { return true; }
|
||
return false;
|
||
}
|
||
|
||
module.exports = { ROOT, load, loadAll, throws };
|