diff --git a/frontend_tests/node_tests/narrow_unread.js b/frontend_tests/node_tests/narrow_unread.js index 394e7c0759..7d71d30245 100644 --- a/frontend_tests/node_tests/narrow_unread.js +++ b/frontend_tests/node_tests/narrow_unread.js @@ -192,7 +192,6 @@ run_test('get_unread_ids', () => { ]; set_filter(terms); - blueslip.set_test_data('warn', 'Unknown emails: bob@example.com'); unread_ids = candidate_ids(); assert.deepEqual(unread_ids, []); diff --git a/frontend_tests/node_tests/zblueslip.js b/frontend_tests/node_tests/zblueslip.js index 098fb37e3c..ac02212ace 100644 --- a/frontend_tests/node_tests/zblueslip.js +++ b/frontend_tests/node_tests/zblueslip.js @@ -30,10 +30,6 @@ run_test('basics', () => { blueslip.error('world'); } - // Let's add an error that we are expecting from the module. - // The function 'set_test_data' pushes the expected error message to the array - // of messages expected for that log type; here, 'error'. - blueslip.set_test_data('error', 'hello'); // Since the error 'world' is not being expected, blueslip will // throw an error. assert.throws(throw_an_error); @@ -66,9 +62,6 @@ run_test('basics', () => { blueslip.warn('world'); } - // Populate one valid value, and test with an invalid value. - // This should throw an error, and we'll assert it was thrown by zblueslip. - blueslip.set_test_data('warn', 'hello'); assert.throws(throw_a_warning); blueslip.clear_test_data(); diff --git a/frontend_tests/zjsunit/index.js b/frontend_tests/zjsunit/index.js index 57c07c849f..bbde68bc37 100644 --- a/frontend_tests/zjsunit/index.js +++ b/frontend_tests/zjsunit/index.js @@ -116,6 +116,11 @@ try { set_global('i18n', stub_i18n); namespace.clear_zulip_refs(); run_one_module(file); + + if (global.blueslip && blueslip.clear_test_data) { + blueslip.clear_test_data(); + } + namespace.restore(); }); } catch (e) { diff --git a/frontend_tests/zjsunit/zblueslip.js b/frontend_tests/zjsunit/zblueslip.js index 36be2d012b..5a83c215fe 100644 --- a/frontend_tests/zjsunit/zblueslip.js +++ b/frontend_tests/zjsunit/zblueslip.js @@ -11,28 +11,41 @@ exports.make_zblueslip = function () { error: true, fatal: true, }; + const names = Array.from(Object.keys(opts)); // Store valid test data for options. lib.test_data = {}; lib.test_logs = {}; - Object.keys(opts).forEach(name => { + lib.seen_messages = {}; + + for (const name of names) { lib.test_data[name] = []; lib.test_logs[name] = []; - }); + lib.seen_messages[name] = new Set(); + } + lib.set_test_data = (name, message) => { lib.test_data[name].push(message); }; - lib.clear_test_data = (name) => { - if (!name) { - // Clear all data - Object.keys(opts).forEach(name => { - lib.test_data[name] = []; - lib.test_logs[name] = []; - }); - return; + + lib.check_seen_messages = () => { + for (const name of names) { + for (const message of lib.test_data[name]) { + if (!lib.seen_messages[name].has(message)) { + throw Error('Never saw: ' + message); + } + } + } + }; + + lib.clear_test_data = () => { + lib.check_seen_messages(); + + for (const name of names) { + lib.test_data[name] = []; + lib.test_logs[name] = []; + lib.seen_messages[name].clear(); } - lib.test_data[name] = []; - lib.test_logs[name] = []; }; lib.get_test_logs = (name) => { @@ -40,13 +53,14 @@ exports.make_zblueslip = function () { }; // Create logging functions - Object.keys(opts).forEach(name => { + for (const name of names) { if (!opts[name]) { // should just log the message. lib[name] = function (message, more_info, stack) { + lib.seen_messages[name].add(message); lib.test_logs[name].push({message, more_info, stack}); }; - return; + continue; } lib[name] = function (message, more_info, stack) { if (typeof message !== 'string') { @@ -58,6 +72,7 @@ exports.make_zblueslip = function () { throw Error('message should be string: ' + message); } } + lib.seen_messages[name].add(message); lib.test_logs[name].push({message, more_info, stack}); const exact_match_fail = !lib.test_data[name].includes(message); if (exact_match_fail) { @@ -66,7 +81,7 @@ exports.make_zblueslip = function () { throw error; } }; - }); + } lib.exception_msg = function (ex) { return ex.message;