mirror of https://github.com/zulip/zulip.git
zjsunit: Lift restriction against mocking CJS modules.
Factor out mock_cjs from mock_esm because adding __esModule prevents mocks for CJS modules from being imported correctly. Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
876806eb4d
commit
5251fa81fb
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue