2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const fs = require("fs");
|
2020-07-24 06:02:07 +02:00
|
|
|
const Module = require("module");
|
|
|
|
const path = require("path");
|
|
|
|
|
2020-07-29 01:37:13 +02:00
|
|
|
const Handlebars = require("handlebars/runtime");
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
2018-04-12 22:50:09 +02:00
|
|
|
|
2020-08-29 04:08:14 +02:00
|
|
|
const handlebars = require("./handlebars");
|
|
|
|
const stub_i18n = require("./i18n");
|
|
|
|
const namespace = require("./namespace");
|
|
|
|
const stub = require("./stub");
|
2020-08-03 23:57:48 +02:00
|
|
|
const {make_zblueslip} = require("./zblueslip");
|
2020-08-29 04:08:14 +02:00
|
|
|
const zjquery = require("./zjquery");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2019-10-12 03:37:06 +02:00
|
|
|
require("@babel/register")({
|
|
|
|
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
|
|
|
|
only: [
|
2020-07-25 02:02:35 +02:00
|
|
|
new RegExp("^" + _.escapeRegExp(path.resolve(__dirname, "../../static/js")) + path.sep),
|
2020-07-15 00:34:28 +02:00
|
|
|
new RegExp(
|
2020-07-25 02:02:35 +02:00
|
|
|
"^" + _.escapeRegExp(path.resolve(__dirname, "../../static/shared/js")) + path.sep,
|
2020-07-15 00:34:28 +02:00
|
|
|
),
|
2019-10-12 03:37:06 +02:00
|
|
|
],
|
|
|
|
plugins: ["rewire-ts"],
|
|
|
|
});
|
2013-08-20 06:15:41 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
global.assert = require("assert").strict;
|
2018-05-31 23:55:28 +02:00
|
|
|
|
2018-04-25 15:25:30 +02:00
|
|
|
// Create a helper function to avoid sneaky delays in tests.
|
|
|
|
function immediate(f) {
|
2020-07-02 01:41:40 +02:00
|
|
|
return () => f();
|
2018-04-25 15:25:30 +02:00
|
|
|
}
|
|
|
|
|
2016-07-30 18:14:42 +02:00
|
|
|
// Find the files we need to run.
|
2020-09-02 21:58:08 +02:00
|
|
|
const files = process.argv.slice(2);
|
2020-02-08 06:23:59 +01:00
|
|
|
if (files.length === 0) {
|
2020-10-07 13:01:09 +02:00
|
|
|
throw new Error("No tests found");
|
2016-07-30 18:14:42 +02:00
|
|
|
}
|
|
|
|
|
2016-07-30 17:00:12 +02:00
|
|
|
// Set up our namespace helpers.
|
2020-07-27 16:06:46 +02:00
|
|
|
global.with_field = namespace.with_field;
|
2016-07-30 17:00:12 +02:00
|
|
|
global.set_global = namespace.set_global;
|
2019-07-25 09:13:22 +02:00
|
|
|
global.patch_builtin = namespace.set_global;
|
2017-08-09 18:26:03 +02:00
|
|
|
global.zrequire = namespace.zrequire;
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
global.reset_module = namespace.reset_module;
|
2016-07-30 20:01:15 +02:00
|
|
|
global.stub_out_jquery = namespace.stub_out_jquery;
|
2017-03-11 21:07:24 +01:00
|
|
|
global.with_overrides = namespace.with_overrides;
|
2016-07-30 17:00:12 +02:00
|
|
|
|
2019-07-25 09:13:22 +02:00
|
|
|
global.window = new Proxy(global, {
|
2020-08-01 03:48:32 +02:00
|
|
|
set: (obj, prop, value) => {
|
|
|
|
namespace.set_global(prop, value);
|
|
|
|
return true;
|
|
|
|
},
|
2019-07-25 09:13:22 +02:00
|
|
|
});
|
|
|
|
global.to_$ = () => window;
|
|
|
|
|
2017-03-11 19:45:10 +01:00
|
|
|
// Set up stub helpers.
|
2020-06-12 14:39:14 +02:00
|
|
|
global.make_stub = stub.make_stub;
|
2017-03-11 19:45:10 +01:00
|
|
|
global.with_stub = stub.with_stub;
|
|
|
|
|
2017-05-23 03:20:15 +02:00
|
|
|
// Set up fake jQuery
|
2020-07-24 06:02:07 +02:00
|
|
|
global.make_zjquery = zjquery.make_zjquery;
|
2017-06-28 10:16:34 +02:00
|
|
|
|
2019-07-11 05:06:20 +02:00
|
|
|
// Set up Handlebars
|
2019-07-12 02:04:01 +02:00
|
|
|
global.stub_templates = handlebars.stub_templates;
|
2019-07-11 05:06:20 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const noop = function () {};
|
2013-08-21 23:53:00 +02:00
|
|
|
|
2017-07-16 21:14:03 +02:00
|
|
|
// Set up fake module.hot
|
2020-05-27 00:38:59 +02:00
|
|
|
Module.prototype.hot = {
|
2017-07-16 21:14:03 +02:00
|
|
|
accept: noop,
|
|
|
|
};
|
|
|
|
|
2018-04-12 22:50:09 +02:00
|
|
|
// Set up fixtures.
|
|
|
|
global.read_fixture_data = (fn) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const full_fn = path.join(__dirname, "../../zerver/tests/fixtures/", fn);
|
|
|
|
const data = JSON.parse(fs.readFileSync(full_fn, "utf8", "r"));
|
2018-04-12 22:50:09 +02:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2018-05-14 21:37:36 +02:00
|
|
|
function short_tb(tb) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const lines = tb.split("\n");
|
2018-05-14 21:37:36 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const i = lines.findIndex(
|
|
|
|
(line) => line.includes("run_test") || line.includes("run_one_module"),
|
|
|
|
);
|
2018-05-14 21:37:36 +02:00
|
|
|
|
|
|
|
if (i === -1) {
|
|
|
|
return tb;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return lines.splice(0, i + 1).join("\n") + "\n(...)\n";
|
2018-05-14 21:37:36 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 01:47:49 +02:00
|
|
|
// Set up Markdown comparison helper
|
2020-08-29 04:08:14 +02:00
|
|
|
global.markdown_assert = require("./markdown_assert");
|
2017-12-10 09:01:37 +01:00
|
|
|
|
2020-07-07 07:11:03 +02:00
|
|
|
let current_file_name;
|
|
|
|
|
2018-05-14 21:37:36 +02:00
|
|
|
function run_one_module(file) {
|
2020-09-02 21:58:08 +02:00
|
|
|
console.info("running test " + path.basename(file, ".js"));
|
|
|
|
current_file_name = file;
|
|
|
|
require(file);
|
2018-05-14 21:37:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
global.run_test = (label, f) => {
|
|
|
|
if (files.length === 1) {
|
2020-07-15 01:29:15 +02:00
|
|
|
console.info(" test: " + label);
|
2018-05-15 12:40:07 +02:00
|
|
|
}
|
2020-07-07 07:11:03 +02:00
|
|
|
try {
|
2020-07-26 16:25:43 +02:00
|
|
|
global.with_overrides(f);
|
2020-07-07 07:11:03 +02:00
|
|
|
} catch (error) {
|
2020-07-15 01:29:15 +02:00
|
|
|
console.info("-".repeat(50));
|
2020-07-07 07:11:03 +02:00
|
|
|
console.info(`test failed: ${current_file_name} > ${label}`);
|
|
|
|
console.info();
|
|
|
|
throw error;
|
|
|
|
}
|
2020-04-20 13:08:44 +02:00
|
|
|
// defensively reset blueslip after each test.
|
|
|
|
blueslip.reset();
|
2018-05-15 12:40:07 +02:00
|
|
|
};
|
|
|
|
|
2018-05-14 21:37:36 +02:00
|
|
|
try {
|
2020-07-02 01:45:54 +02:00
|
|
|
files.forEach((file) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("location", {
|
|
|
|
hash: "#",
|
2019-07-25 09:13:22 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
global.patch_builtin("setTimeout", noop);
|
|
|
|
global.patch_builtin("setInterval", noop);
|
2018-05-14 21:37:36 +02:00
|
|
|
_.throttle = immediate;
|
|
|
|
_.debounce = immediate;
|
|
|
|
|
2020-08-03 23:57:48 +02:00
|
|
|
set_global("blueslip", make_zblueslip());
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("i18n", stub_i18n);
|
2020-02-27 15:40:59 +01:00
|
|
|
namespace.clear_zulip_refs();
|
2020-04-03 18:08:38 +02:00
|
|
|
|
2018-05-14 21:37:36 +02:00
|
|
|
run_one_module(file);
|
2020-04-03 16:41:13 +02:00
|
|
|
|
2020-04-03 18:08:38 +02:00
|
|
|
if (blueslip.reset) {
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.reset();
|
2020-04-03 16:41:13 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 21:37:36 +02:00
|
|
|
namespace.restore();
|
2020-07-29 01:37:13 +02:00
|
|
|
Handlebars.HandlebarsEnvironment();
|
2018-05-14 21:37:36 +02:00
|
|
|
});
|
2020-10-07 10:20:41 +02:00
|
|
|
} catch (error) {
|
|
|
|
if (error.stack) {
|
|
|
|
console.info(short_tb(error.stack));
|
2018-05-14 21:37:36 +02:00
|
|
|
} else {
|
2020-10-07 10:20:41 +02:00
|
|
|
console.info(error);
|
2018-05-14 21:37:36 +02:00
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|