From aab541bd4e35b4b25bbe2122ecb43552b74a9ad5 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 8 Nov 2024 02:50:17 -0800 Subject: [PATCH] namespace: Recognize immutable ES modules without checking __Rewire__. Signed-off-by: Anders Kaseorg --- web/tests/lib/namespace.js | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/web/tests/lib/namespace.js b/web/tests/lib/namespace.js index 943aa6c579..f684be12b7 100644 --- a/web/tests/lib/namespace.js +++ b/web/tests/lib/namespace.js @@ -26,26 +26,19 @@ let jquery_function; const template_path = path.resolve(__dirname, "../../templates"); function load(request, parent, isMain) { - let module; - const filename = Module._resolveFilename(request, parent, isMain); if (module_mocks.has(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)) { 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) { - module = jquery_function || $; - } else { - module = actual_load(request, parent, isMain); + return jquery_function || $; } - if ( - (typeof module === "object" || typeof module === "function") && - "__esModule" in module && - "__Rewire__" in module - ) { + const module = actual_load(request, parent, isMain); + if ((typeof module === "object" || typeof module === "function") && "__esModule" in module) { /* istanbul ignore next */ function error_immutable() { 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`, ); - 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 old_value = obj[prop]; let new_value = value;