Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zjsunit: Use clear_zulip_refs as the main way to undo zrequire.
Anders Kaseorg0ca543396a49
diff --git a/frontend_tests/zjsunit/index.js b/frontend_tests/zjsunit/index.jsindex 9804eff90ed..59caa6cbbb5 100644--- a/frontend_tests/zjsunit/index.js+++ b/frontend_tests/zjsunit/index.js@@ -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);diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.jsindex d1b93a16549..83c68586699 100644--- a/frontend_tests/zjsunit/namespace.js+++ b/frontend_tests/zjsunit/namespace.js@@ -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 = {};