mirror of https://github.com/zulip/zulip.git
zjsunit: Restrict overrides to once-per-function.
We can relax this restriction in the future, but basically every time it came up for me, the test code was just disorganized, or it had an easy workaround.
This commit is contained in:
parent
90730a18d9
commit
16ad65dd66
|
@ -96,15 +96,31 @@ exports.with_overrides = function (test_function) {
|
|||
|
||||
const restore_callbacks = [];
|
||||
const unused_funcs = new Map();
|
||||
const funcs = new Map();
|
||||
|
||||
const override = function (module, func_name, f) {
|
||||
if (typeof f !== "function") {
|
||||
throw new TypeError("You can only override with a function.");
|
||||
}
|
||||
|
||||
if (!funcs.has(module)) {
|
||||
funcs.set(module, new Map());
|
||||
}
|
||||
|
||||
if (funcs.get(module).has(func_name)) {
|
||||
// Prevent overriding the same function twice, so that
|
||||
// it's super easy to reason about our logic to restore
|
||||
// the original function. Usually if somebody sees this
|
||||
// error, it's a symptom of not breaking up tests enough.
|
||||
throw new Error("You can only override a function one time.");
|
||||
}
|
||||
|
||||
funcs.get(module).set(func_name, true);
|
||||
|
||||
if (!unused_funcs.has(module)) {
|
||||
unused_funcs.set(module, new Map());
|
||||
}
|
||||
|
||||
unused_funcs.get(module).set(func_name, true);
|
||||
|
||||
const old_f = module[func_name];
|
||||
|
|
Loading…
Reference in New Issue