Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zjquery: Prevent direct patches to $.
Steve Howell89ab76920fb4
diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.jsindex 60601ea6cbc..fe54c5793ab 100644--- a/frontend_tests/zjsunit/namespace.js+++ b/frontend_tests/zjsunit/namespace.js@@ -122,14 +122,31 @@ exports.with_overrides = function (test_function) {unused_funcs.get(obj).set(func_name, true);- const old_f = obj[func_name];- obj[func_name] = function (...args) {+ let old_f = obj[func_name];+ if (old_f === undefined) {+ // Create a dummy function so that we can+ // attach _patched_with_override to it.+ old_f = () => {+ throw new Error(`There is no ${func_name}() field for this object.`);+ };+ }++ const new_f = function (...args) {unused_funcs.get(obj).delete(func_name);return f.apply(this, args);};+ // Let zjquery know this function was patched with override,+ // so it doesn't complain about us modifying it. (Other+ // 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;});};diff --git a/frontend_tests/zjsunit/zjquery.js b/frontend_tests/zjsunit/zjquery.jsindex d376d0df726..a5c1196db2b 100644--- a/frontend_tests/zjsunit/zjquery.js+++ b/frontend_tests/zjsunit/zjquery.js@@ -558,4 +558,32 @@ function make_zjquery() {return zjquery;}-module.exports = make_zjquery();+const $ = new Proxy(make_zjquery(), {+ set(obj, prop, value) {+ if (value._patched_with_override) {+ obj[prop] = value;+ return true;+ }++ throw new Error(`+ Please don't modify $.${prop} if you are using zjquery.++ You can do this instead:++ override($, "${prop}", () => {...});++ Or you can do this if you don't actually+ need zjquery and just want to simulate one function.++ set_global("$", {+ ${prop}(...) {...},+ });++ It's also possible that you are testing code with+ node tests when it would be a better strategy to+ use puppeteer tests.+ `);+ },+});++module.exports = $;