Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zjsunit: Prevent common mistakes with overrides.
Steve Howell914533c8bc1a
diff --git a/frontend_tests/node_tests/message_edit.js b/frontend_tests/node_tests/message_edit.jsindex 79ca458b3c1..9c5c8a6e5ea 100644--- a/frontend_tests/node_tests/message_edit.js+++ b/frontend_tests/node_tests/message_edit.js@@ -5,7 +5,6 @@ const {strict: assert} = require("assert");const {set_global, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");-set_global("document", null);set_global("page_params", {realm_community_topic_editing_limit_seconds: 259200,});diff --git a/frontend_tests/node_tests/message_list_view.js b/frontend_tests/node_tests/message_list_view.jsindex f17e46d523e..8ea9c7a2888 100644--- a/frontend_tests/node_tests/message_list_view.js+++ b/frontend_tests/node_tests/message_list_view.js@@ -20,7 +20,7 @@ const noop = function () {};set_global("page_params", {twenty_four_hour_time: false,});-set_global("home_msg_list", null);+set_global("home_msg_list", "stub");set_global("unread", {message_unread() {}});// timerender calls setInterval when importedset_global("timerender", {diff --git a/frontend_tests/node_tests/tutorial.js b/frontend_tests/node_tests/tutorial.jsindex fd5e42cbede..cf44a38af28 100644--- a/frontend_tests/node_tests/tutorial.js+++ b/frontend_tests/node_tests/tutorial.js@@ -620,8 +620,9 @@ run_test("unread_ops", (override) => {});// Ignore these interactions for now:- message_list.all = {};- override(message_list.all, "show_message_as_read", noop);+ message_list.all = {+ show_message_as_read() {},+ };override(home_msg_list, "show_message_as_read", noop);override(notifications, "close_notification", noop);})();diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.jsindex 40fff8db7e3..d1b93a16549 100644--- a/frontend_tests/zjsunit/namespace.js+++ b/frontend_tests/zjsunit/namespace.js@@ -9,6 +9,18 @@ const new_globals = new Set();let old_globals = {};exports.set_global = function (name, val) {+ if (val === null) {+ throw new Error(`+ We try to avoid using null in our codebase.+ `);+ }++ // Add this for debugging and to allow with_overrides+ // to know that we're dealing with stubbed code.+ if (typeof val === "object") {+ val._patched_with_set_global = true;+ }+if (!(name in old_globals)) {if (!(name in global)) {new_globals.add(name);@@ -101,6 +113,26 @@ exports.with_overrides = function (test_function) {);}+ if (typeof obj !== "object" && typeof obj !== "function") {+ throw new TypeError(`We cannot override a function for ${typeof obj} objects`);+ }++ if (obj[func_name] === undefined) {+ if (obj !== global.$ && !obj._patched_with_set_global) {+ throw new Error(`+ It looks like you are overriding ${func_name}+ for a module that never defined it, which probably+ indicates that you have a typo or are doing+ something hacky in the test.+ `);+ }+ } else if (typeof obj[func_name] !== "function") {+ throw new TypeError(`+ You are overriding a non-function with a function.+ This is almost certainly an error.+ `);+ }+if (!funcs.has(obj)) {funcs.set(obj, new Map());}