zjsunit: Use clear_zulip_refs as the main way to undo zrequire.

Move clear_zulip_refs into restore, and rewrite it without lodash.  We
no longer need the requires array, and zrequire is now nothing more
than a wrapper around require.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-22 10:51:28 -08:00
parent 7011f0911d
commit 0ca543396a
2 changed files with 7 additions and 26 deletions

View File

@ -90,7 +90,6 @@ try {
const blueslip = namespace.set_global("blueslip", make_zblueslip());
namespace.set_global("i18n", stub_i18n);
namespace.clear_zulip_refs();
run_one_module(file);

View File

@ -2,9 +2,6 @@
const path = require("path");
const _ = require("lodash");
const requires = [];
const new_globals = new Set();
let old_globals = {};
@ -43,9 +40,7 @@ function require_path(name, fn) {
}
exports.zrequire = function (name, fn) {
fn = require_path(name, fn);
requires.push(fn);
return require(fn);
return require(require_path(name, fn));
};
exports.reset_module = function (name, fn) {
@ -54,27 +49,14 @@ exports.reset_module = function (name, fn) {
return require(fn);
};
exports.clear_zulip_refs = function () {
/*
This is a big hammer to make sure
we are not "borrowing" a transitively
required module from a previous test.
This kind of leak can make it seems
like we've written the second test
correctly, but it will fail if we
run it standalone.
*/
const staticPath = path.resolve(__dirname, "../../static") + path.sep;
_.each(require.cache, (_, fn) => {
if (fn.startsWith(staticPath) && !fn.startsWith(staticPath + "templates" + path.sep)) {
delete require.cache[fn];
}
});
};
const staticPath = path.resolve(__dirname, "../../static") + path.sep;
const templatesPath = staticPath + "templates" + path.sep;
exports.restore = function () {
for (const fn of requires) {
delete require.cache[require.resolve(fn)];
for (const path of Object.keys(require.cache)) {
if (path.startsWith(staticPath) && !path.startsWith(templatesPath)) {
delete require.cache[path];
}
}
Object.assign(global, old_globals);
old_globals = {};