namespace: Recognize immutable ES modules without checking __Rewire__.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-11-08 02:50:17 -08:00 committed by Tim Abbott
parent 8c60b66a95
commit aab541bd4e
1 changed files with 5 additions and 17 deletions

View File

@ -26,26 +26,19 @@ let jquery_function;
const template_path = path.resolve(__dirname, "../../templates"); const template_path = path.resolve(__dirname, "../../templates");
function load(request, parent, isMain) { function load(request, parent, isMain) {
let module;
const filename = Module._resolveFilename(request, parent, isMain); const filename = Module._resolveFilename(request, parent, isMain);
if (module_mocks.has(filename)) { if (module_mocks.has(filename)) {
used_module_mocks.add(filename); used_module_mocks.add(filename);
module = module_mocks.get(filename); return module_mocks.get(filename);
} else if (filename.endsWith(".hbs") && filename.startsWith(template_path + path.sep)) { } else if (filename.endsWith(".hbs") && filename.startsWith(template_path + path.sep)) {
const actual_render = actual_load(request, parent, isMain); const actual_render = actual_load(request, parent, isMain);
module = template_stub({filename, actual_render}); return template_stub({filename, actual_render});
} else if (filename === jquery_path && parent.filename !== real_jquery_path) { } else if (filename === jquery_path && parent.filename !== real_jquery_path) {
module = jquery_function || $; return jquery_function || $;
} else {
module = actual_load(request, parent, isMain);
} }
if ( const module = actual_load(request, parent, isMain);
(typeof module === "object" || typeof module === "function") && if ((typeof module === "object" || typeof module === "function") && "__esModule" in module) {
"__esModule" in module &&
"__Rewire__" in module
) {
/* istanbul ignore next */ /* istanbul ignore next */
function error_immutable() { function error_immutable() {
throw new Error(`${filename} is an immutable ES module`); throw new Error(`${filename} is an immutable ES module`);
@ -289,11 +282,6 @@ exports.with_overrides = function (test_function) {
`We cannot override a function for ${typeof obj} objects`, `We cannot override a function for ${typeof obj} objects`,
); );
assert.ok(
!("__esModule" in obj && "__Rewire__" in obj),
"Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",
);
const had_value = Object.hasOwn(obj, prop); const had_value = Object.hasOwn(obj, prop);
const old_value = obj[prop]; const old_value = obj[prop];
let new_value = value; let new_value = value;