zjsunit: Use __Rewire__ for ES modules in override.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-19 20:15:54 -08:00 committed by Tim Abbott
parent a6510789c9
commit 8985b5ffc7
1 changed files with 17 additions and 8 deletions

View File

@ -144,7 +144,10 @@ exports.with_overrides = function (test_function) {
unused_funcs.get(obj).set(func_name, true);
let old_f = obj[func_name];
let old_f =
"__esModule" in obj && "__Rewire__" in obj && !(func_name in obj)
? obj.__GetDependency__(func_name)
: obj[func_name];
if (old_f === undefined) {
// Create a dummy function so that we can
// attach _patched_with_override to it.
@ -163,13 +166,19 @@ exports.with_overrides = function (test_function) {
// code can also use this, as needed.)
new_f._patched_with_override = true;
obj[func_name] = new_f;
restore_callbacks.push(() => {
old_f._patched_with_override = true;
obj[func_name] = old_f;
delete old_f._patched_with_override;
});
if ("__esModule" in obj && "__Rewire__" in obj) {
obj.__Rewire__(func_name, new_f);
restore_callbacks.push(() => {
obj.__Rewire__(func_name, old_f);
});
} else {
obj[func_name] = new_f;
restore_callbacks.push(() => {
old_f._patched_with_override = true;
obj[func_name] = old_f;
delete old_f._patched_with_override;
});
}
};
try {