2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
|
|
|
|
2018-04-09 10:01:24 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
This test module actually tests our test code, particularly zblueslip, and
|
|
|
|
it is intended to demonstrate how to use zblueslip (as well as, of course,
|
|
|
|
verify that it works as advertised).
|
|
|
|
|
|
|
|
What is zblueslip?
|
|
|
|
|
|
|
|
The zblueslip test module behaves like blueslip at a very surface level,
|
|
|
|
and it allows you to test code that uses actual blueslip and add some
|
|
|
|
custom validation for checking that only particular errors and warnings are
|
|
|
|
thrown by our test modules.
|
|
|
|
|
2020-04-03 18:08:38 +02:00
|
|
|
The test runner automatically replaces `blueslip` with an instance
|
|
|
|
of a zblueslip object.
|
|
|
|
|
2018-04-09 10:01:24 +02:00
|
|
|
The code we are testing lives here:
|
|
|
|
|
|
|
|
https://github.com/zulip/zulip/blob/master/frontend_tests/zjsunit/zblueslip.js
|
|
|
|
|
2018-04-24 00:31:22 +02:00
|
|
|
Read the following contents for an overview of how zblueslip works. Also take a
|
|
|
|
look at `node_tests/people_errors.js` for actual usage of this module.
|
2018-04-09 10:01:24 +02:00
|
|
|
*/
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basics", () => {
|
2018-04-09 10:01:24 +02:00
|
|
|
// Let's create a sample piece of code to test:
|
|
|
|
function throw_an_error() {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("world");
|
2018-04-09 10:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since the error 'world' is not being expected, blueslip will
|
2018-04-24 00:31:22 +02:00
|
|
|
// throw an error.
|
2018-04-09 10:01:24 +02:00
|
|
|
assert.throws(throw_an_error);
|
2018-04-24 00:31:22 +02:00
|
|
|
// zblueslip logs all the calls made to it, and they can be used in asserts like:
|
2018-04-09 10:01:24 +02:00
|
|
|
|
|
|
|
// Now, let's add our error to the list of expected errors.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "world", 2);
|
2018-04-09 10:01:24 +02:00
|
|
|
// This time, blueslip will just log the error, which is
|
|
|
|
// being verified by the assert call on the length of the log.
|
|
|
|
// We can also check for which specific error was logged, but since
|
|
|
|
// our sample space is just 1 expected error, we are sure that
|
|
|
|
// only that error could have been logged, and others would raise
|
|
|
|
// an error, aborting the test.
|
|
|
|
throw_an_error();
|
2020-04-20 12:38:24 +02:00
|
|
|
// The following check is redundant; blueslip.reset() already asserts that
|
|
|
|
// we got the expected number of errors.
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(blueslip.get_test_logs("error").length, 2);
|
2018-04-09 10:01:24 +02:00
|
|
|
|
|
|
|
// Let's clear the array of valid errors as well as the log. Now, all errors
|
|
|
|
// should be thrown directly by blueslip.
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.reset();
|
2018-04-09 10:01:24 +02:00
|
|
|
assert.throws(throw_an_error);
|
2020-04-20 12:38:24 +02:00
|
|
|
// This call to blueslip.reset() would complain.
|
|
|
|
assert.throws(() => {
|
|
|
|
blueslip.reset();
|
|
|
|
});
|
2018-04-09 10:01:24 +02:00
|
|
|
|
|
|
|
// Let's repeat the above procedue with warnings. Unlike errors,
|
2020-03-28 01:25:56 +01:00
|
|
|
// warnings shouldn't stop the code execution, and thus, the
|
2018-04-09 10:01:24 +02:00
|
|
|
// behaviour is slightly different.
|
|
|
|
|
|
|
|
function throw_a_warning() {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("world");
|
2018-04-09 10:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.throws(throw_a_warning);
|
2020-04-20 12:38:24 +02:00
|
|
|
// Again, we do not expect this particular warning so blueslip.reset should complain.
|
|
|
|
assert.throws(() => {
|
|
|
|
blueslip.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Let's reset blueslip regardless of errors. This is only for demonstration
|
|
|
|
// purposes here; do not reset blueslip like this in actual tests.
|
|
|
|
blueslip.reset(true);
|
2018-04-09 10:01:24 +02:00
|
|
|
|
|
|
|
// Now, let's add our warning to the list of expected warnings.
|
|
|
|
// This time, we shouldn't throw an error. However, to confirm that we
|
|
|
|
// indeed had logged a warning, we can check the length of the warning logs
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "world");
|
2018-04-09 10:01:24 +02:00
|
|
|
throw_a_warning();
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.reset();
|
2020-04-20 12:38:24 +02:00
|
|
|
|
|
|
|
// However, we detect when we have more or less of the expected errors/warnings.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "world");
|
2020-04-20 12:38:24 +02:00
|
|
|
assert.throws(() => {
|
|
|
|
blueslip.reset();
|
|
|
|
});
|
|
|
|
// Again, forcefully reset blueslip.
|
|
|
|
blueslip.reset(true);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|