Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zjsunit: Split deprecated __Rewire__ functionality out of with_field.
Anders Kaseorge17ba5260a44
diff --git a/frontend_tests/node_tests/buddy_data.js b/frontend_tests/node_tests/buddy_data.jsindex 69833b3b72a..026812b6f8d 100644--- a/frontend_tests/node_tests/buddy_data.js+++ b/frontend_tests/node_tests/buddy_data.js@@ -4,7 +4,7 @@ const {strict: assert} = require("assert");const _ = require("lodash");-const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");+const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");const {page_params, user_settings} = require("../zjsunit/zpage_params");@@ -456,7 +456,7 @@ test("bulk_data_hacks", () => {// Make our shrink limit higher, and go back to an empty search.// We won't get all 1000 users, just the present ones.- with_field(buddy_data, "max_size_before_shrinking", 50000, () => {+ with_field_rewire(buddy_data, "max_size_before_shrinking", 50000, () => {user_ids = buddy_data.get_filtered_and_sorted_user_ids("");});assert.equal(user_ids.length, 700);diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.jsindex 19bbe7c8030..b98e6a18f51 100644--- a/frontend_tests/node_tests/composebox_typeahead.js+++ b/frontend_tests/node_tests/composebox_typeahead.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");+const {mock_esm, set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const $ = require("../zjsunit/zjquery");const {page_params, user_settings} = require("../zjsunit/zpage_params");@@ -433,9 +433,14 @@ test("content_typeahead_selected", ({override_rewire}) => {fake_this.query = "@back";fake_this.token = "back";- with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {- actual_value = ct.content_typeahead_selected.call(fake_this, backend);- });+ with_field_rewire(+ compose_validate,+ "warn_if_mentioning_unsubscribed_user",+ unexpected_warn,+ () => {+ actual_value = ct.content_typeahead_selected.call(fake_this, backend);+ },+ );expected_value = "@*Backend* ";assert.equal(actual_value, expected_value);@@ -453,9 +458,14 @@ test("content_typeahead_selected", ({override_rewire}) => {fake_this.query = "@_kin";fake_this.token = "kin";- with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {- actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);- });+ with_field_rewire(+ compose_validate,+ "warn_if_mentioning_unsubscribed_user",+ unexpected_warn,+ () => {+ actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);+ },+ );expected_value = "@_**King Hamlet** ";assert.equal(actual_value, expected_value);@@ -480,9 +490,14 @@ test("content_typeahead_selected", ({override_rewire}) => {fake_this.query = "@_back";fake_this.token = "back";- with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {- actual_value = ct.content_typeahead_selected.call(fake_this, backend);- });+ with_field_rewire(+ compose_validate,+ "warn_if_mentioning_unsubscribed_user",+ unexpected_warn,+ () => {+ actual_value = ct.content_typeahead_selected.call(fake_this, backend);+ },+ );expected_value = "@_*Backend* ";assert.equal(actual_value, expected_value);@@ -1578,7 +1593,7 @@ test("message people", ({override}) => {};function get_results(search_key) {- return with_field(ct, "max_num_items", 2, () =>+ return with_field_rewire(ct, "max_num_items", 2, () =>ct.get_person_suggestions(search_key, opts),);}diff --git a/frontend_tests/node_tests/markdown.js b/frontend_tests/node_tests/markdown.jsindex 1ab09c1b97c..88263406f2f 100644--- a/frontend_tests/node_tests/markdown.js+++ b/frontend_tests/node_tests/markdown.js@@ -4,7 +4,7 @@ const {strict: assert} = require("assert");const markdown_test_cases = require("../../zerver/tests/fixtures/markdown_test_cases");const markdown_assert = require("../zjsunit/markdown_assert");-const {set_global, with_field, zrequire} = require("../zjsunit/namespace");+const {set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");const {page_params, user_settings} = require("../zjsunit/zpage_params");@@ -833,7 +833,7 @@ test("missing unicode emojis", ({override_rewire}) => {test("katex_throws_unexpected_exceptions", () => {blueslip.expect("error", "Error: some-exception");const message = {raw_content: "$$a$$"};- with_field(+ with_field_rewire(markdown,"katex",{diff --git a/frontend_tests/node_tests/message_flags.js b/frontend_tests/node_tests/message_flags.jsindex 3598b977e14..e2f52df4148 100644--- a/frontend_tests/node_tests/message_flags.js+++ b/frontend_tests/node_tests/message_flags.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");+const {mock_esm, set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const channel = mock_esm("../../static/js/channel");@@ -151,7 +151,7 @@ run_test("read", ({override}) => {// For testing purpose limit the batch size value to 5 instead of 1000function send_read(messages) {- with_field(message_flags, "_unread_batch_size", 5, () => {+ with_field_rewire(message_flags, "_unread_batch_size", 5, () => {message_flags.send_read(messages);});}@@ -270,7 +270,7 @@ run_test("read_empty_data", ({override}) => {// For testing purpose limit the batch size value to 5 instead of 1000function send_read(messages) {- with_field(message_flags, "_unread_batch_size", 5, () => {+ with_field_rewire(message_flags, "_unread_batch_size", 5, () => {message_flags.send_read(messages);});}diff --git a/frontend_tests/node_tests/message_list_data.js b/frontend_tests/node_tests/message_list_data.jsindex d0f08020b44..a9d05981596 100644--- a/frontend_tests/node_tests/message_list_data.js+++ b/frontend_tests/node_tests/message_list_data.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {set_global, with_field, zrequire} = require("../zjsunit/namespace");+const {set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");@@ -129,7 +129,7 @@ run_test("muting", () => {// `messages_filtered_for_topic_mutes` should skip filtering// messages if `excludes_muted_topics` is false.- with_field(+ with_field_rewire(muted_topics,"is_topic_muted",() => {@@ -145,7 +145,7 @@ run_test("muting", () => {// If we are in a 1:1 PM narrow, `messages_filtered_for_user_mutes` should skip// filtering messages.- with_field(+ with_field_rewire(muted_topics,"is_user_muted",() => {diff --git a/frontend_tests/node_tests/message_store.js b/frontend_tests/node_tests/message_store.jsindex c40659fd6e9..98d7564bea0 100644--- a/frontend_tests/node_tests/message_store.js+++ b/frontend_tests/node_tests/message_store.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");+const {mock_esm, set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");const {page_params} = require("../zjsunit/zpage_params");@@ -227,7 +227,7 @@ test("errors", () => {};// This should early return and not run pm_conversations.set_partner- with_field(+ with_field_rewire(pm_conversations,"set_partner",() => assert.fail(),diff --git a/frontend_tests/node_tests/narrow.js b/frontend_tests/node_tests/narrow.jsindex 135f830a0c8..cea1a064f9e 100644--- a/frontend_tests/node_tests/narrow.js+++ b/frontend_tests/node_tests/narrow.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {with_field, zrequire} = require("../zjsunit/namespace");+const {with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const $ = require("../zjsunit/zjquery");const {page_params} = require("../zjsunit/zpage_params");@@ -550,7 +550,7 @@ run_test("show_invalid_narrow_message", ({mock_template}) => {run_test("narrow_to_compose_target errors", () => {function test() {- with_field(+ with_field_rewire(narrow,"activate",() => {diff --git a/frontend_tests/node_tests/pm_list.js b/frontend_tests/node_tests/pm_list.jsindex 7eaab9b5c1c..478d70a82f8 100644--- a/frontend_tests/node_tests/pm_list.js+++ b/frontend_tests/node_tests/pm_list.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");+const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const $ = require("../zjsunit/zjquery");@@ -268,7 +268,7 @@ test("ensure coverage", ({override}) => {// where functions early exit.override(narrow_state, "active", () => false);- with_field(+ with_field_rewire(pm_list,"rebuild_recent",() => {diff --git a/frontend_tests/node_tests/presence.js b/frontend_tests/node_tests/presence.jsindex 7813a52b98a..3e4b6854298 100644--- a/frontend_tests/node_tests/presence.js+++ b/frontend_tests/node_tests/presence.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");+const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");const {user_settings} = require("../zjsunit/zpage_params");@@ -100,7 +100,7 @@ test("unknown user", ({override}) => {// If the server is suspected to be offline or reloading,// then we suppress errors. The use case here is that we// haven't gotten info for a brand new user yet.- with_field(+ with_field_rewire(watchdog,"suspects_user_is_offline",() => true,diff --git a/frontend_tests/node_tests/stream_topic_history.js b/frontend_tests/node_tests/stream_topic_history.jsindex 1a190cb5184..35e0cc0fadb 100644--- a/frontend_tests/node_tests/stream_topic_history.js+++ b/frontend_tests/node_tests/stream_topic_history.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");+const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const channel = mock_esm("../../static/js/channel");@@ -137,7 +137,7 @@ test("is_complete_for_stream_id", () => {first: () => ({id: 5}),};- with_field(all_messages_data, "all_messages_data", all_messages_data_stub, () => {+ with_field_rewire(all_messages_data, "all_messages_data", all_messages_data_stub, () => {assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);// Now simulate a more recent message id.diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.jsindex 4aa2c211453..eb34b9e3763 100644--- a/frontend_tests/zjsunit/namespace.js+++ b/frontend_tests/zjsunit/namespace.js@@ -322,29 +322,40 @@ exports.finish = function () {exports.with_field = function (obj, field, val, f) {if ("__esModule" in obj && "__Rewire__" in obj) {- const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);- try {- obj.__Rewire__(field, val);- return f();- } finally {- obj.__Rewire__(field, old_val);- }- } else {- const had_val = Object.prototype.hasOwnProperty.call(obj, field);- const old_val = obj[field];- try {- obj[field] = val;- return f();- } finally {- if (had_val) {- obj[field] = old_val;- } else {- delete obj[field];- }+ throw new TypeError(+ "Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",+ );+ }++ const had_val = Object.prototype.hasOwnProperty.call(obj, field);+ const old_val = obj[field];+ try {+ obj[field] = val;+ return f();+ } finally {+ if (had_val) {+ obj[field] = old_val;+ } else {+ delete obj[field];}}};+exports.with_field_rewire = function (obj, field, val, f) {+ // This is deprecated because it relies on the slow+ // babel-plugin-rewire-ts plugin. Consider alternatives such+ // as exporting a helper function for tests from the module+ // containing the function you need to mock.++ const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);+ try {+ obj.__Rewire__(field, val);+ return f();+ } finally {+ obj.__Rewire__(field, old_val);+ }+};+exports.with_overrides = function (test_function) {// This function calls test_function() and passes in// a way to override the namespace temporarily.