Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

zjsunit: Lift restriction against mocking CJS modules.

Anders Kaseorg5251fa81fbc0
diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.js
index 73f5017f2cd..101ab8bc912 100644
--- a/frontend_tests/zjsunit/namespace.js
+++ b/frontend_tests/zjsunit/namespace.js
@@ -30,11 +30,7 @@ exports.start = () => {
Module._load = load;
};
-exports.mock_esm = (request, obj = {}) => {
- if (typeof obj !== "object") {
- throw new TypeError("An ES module must be mocked with an object");
- }
-
+exports.mock_cjs = (request, obj) => {
const filename = Module._resolveFilename(
request,
require.cache[callsites()[1].getFileName()],
@@ -49,11 +45,17 @@ exports.mock_esm = (request, obj = {}) => {
throw new Error(`It is too late to mock ${filename}; call this earlier.`);
}
- obj.__esModule = true;
module_mocks.set(filename, obj);
return obj;
};
+exports.mock_esm = (request, obj = {}) => {
+ if (typeof obj !== "object") {
+ throw new TypeError("An ES module must be mocked with an object");
+ }
+ return exports.mock_cjs(request, {...obj, __esModule: true});
+};
+
exports.unmock_module = (request) => {
const filename = Module._resolveFilename(
request,