2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
const {strict: assert} = require("assert");
|
2020-07-24 06:02:07 +02:00
|
|
|
const path = require("path");
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
require("css.escape");
|
2021-06-15 14:54:53 +02:00
|
|
|
require("handlebars/runtime");
|
2023-05-03 19:28:08 +02:00
|
|
|
const Sentry = require("@sentry/browser");
|
2021-06-15 14:54:53 +02:00
|
|
|
const {JSDOM} = require("jsdom");
|
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");
|
2020-12-01 00:39:47 +01:00
|
|
|
const test = require("./test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("./zblueslip");
|
2021-03-11 05:43:45 +01:00
|
|
|
const zjquery = require("./zjquery");
|
2023-03-18 19:01:20 +01:00
|
|
|
const zpage_billing_params = require("./zpage_billing_params");
|
2021-03-25 22:35:45 +01:00
|
|
|
const zpage_params = require("./zpage_params");
|
2021-02-21 15:38:51 +01:00
|
|
|
|
2022-05-04 07:52:06 +02:00
|
|
|
process.env.NODE_ENV = "test";
|
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
const dom = new JSDOM("", {url: "http://zulip.zulipdev.com/"});
|
|
|
|
global.DOMParser = dom.window.DOMParser;
|
2024-05-14 20:59:07 +02:00
|
|
|
global.HTMLAnchorElement = dom.window.HTMLAnchorElement;
|
|
|
|
global.HTMLElement = dom.window.HTMLElement;
|
2024-06-07 19:39:33 +02:00
|
|
|
global.Window = dom.window.Window;
|
2022-09-11 22:09:18 +02:00
|
|
|
global.navigator = {
|
|
|
|
userAgent: "node.js",
|
|
|
|
};
|
2021-06-15 14:54:53 +02:00
|
|
|
|
2019-10-12 03:37:06 +02:00
|
|
|
require("@babel/register")({
|
|
|
|
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
|
|
|
|
only: [
|
2023-02-22 23:04:10 +01:00
|
|
|
new RegExp("^" + _.escapeRegExp(path.resolve(__dirname, "../../shared/src") + path.sep)),
|
|
|
|
new RegExp("^" + _.escapeRegExp(path.resolve(__dirname, "../../src") + path.sep)),
|
2019-10-12 03:37:06 +02:00
|
|
|
],
|
2021-02-27 20:28:33 +01:00
|
|
|
plugins: [
|
2023-02-24 20:46:13 +01:00
|
|
|
...(process.env.USING_INSTRUMENTED_CODE ? [["istanbul", {exclude: []}]] : []),
|
2021-02-27 20:28:33 +01:00
|
|
|
"babel-plugin-rewire-ts",
|
|
|
|
["@babel/plugin-transform-modules-commonjs", {lazy: () => true}],
|
|
|
|
],
|
2023-02-24 02:20:53 +01:00
|
|
|
root: path.resolve(__dirname, "../.."),
|
2019-10-12 03:37:06 +02:00
|
|
|
});
|
2013-08-20 06:15:41 +02:00
|
|
|
|
2023-05-03 19:28:08 +02:00
|
|
|
// Ensure that startTransaction and friends are available at runtime
|
|
|
|
Sentry.addTracingExtensions();
|
|
|
|
|
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);
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.notEqual(files.length, 0, "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-12-01 00:42:45 +01:00
|
|
|
const window = new Proxy(global, {
|
2023-06-29 21:59:08 +02:00
|
|
|
set(_obj, prop, value) {
|
2020-08-01 03:48:32 +02:00
|
|
|
namespace.set_global(prop, value);
|
|
|
|
return true;
|
|
|
|
},
|
2019-07-25 09:13:22 +02:00
|
|
|
});
|
|
|
|
|
2022-04-10 02:16:59 +02:00
|
|
|
const ls_container = new Map();
|
|
|
|
const localStorage = {
|
|
|
|
getItem(key) {
|
|
|
|
return ls_container.get(key);
|
|
|
|
},
|
|
|
|
setItem(key, val) {
|
|
|
|
ls_container.set(key, val);
|
|
|
|
},
|
|
|
|
/* istanbul ignore next */
|
|
|
|
removeItem(key) {
|
|
|
|
ls_container.delete(key);
|
|
|
|
},
|
|
|
|
clear() {
|
|
|
|
ls_container.clear();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-11 05:06:20 +02:00
|
|
|
// Set up Handlebars
|
2020-12-01 00:19:42 +01:00
|
|
|
handlebars.hook_require();
|
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
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
require("../../src/templates"); // register Zulip extensions
|
2021-10-13 09:41:02 +02:00
|
|
|
|
2022-08-18 02:13:22 +02:00
|
|
|
async function run_one_module(file) {
|
2021-03-13 15:49:01 +01:00
|
|
|
zjquery.clear_initialize_function();
|
2021-03-11 05:43:45 +01:00
|
|
|
zjquery.clear_all_elements();
|
2023-02-22 23:04:10 +01:00
|
|
|
console.info("running test " + path.basename(file, ".test.js"));
|
2020-12-01 00:39:47 +01:00
|
|
|
test.set_current_file_name(file);
|
2022-05-07 02:53:53 +02:00
|
|
|
test.suite.length = 0;
|
2020-09-02 21:58:08 +02:00
|
|
|
require(file);
|
2022-05-07 02:53:53 +02:00
|
|
|
for (const f of test.suite) {
|
2022-08-18 02:13:22 +02:00
|
|
|
await f();
|
2022-05-07 02:53:53 +02:00
|
|
|
}
|
2021-03-13 14:14:40 +01:00
|
|
|
namespace.complain_about_unused_mocks();
|
2018-05-14 21:37:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 00:39:47 +01:00
|
|
|
test.set_verbose(files.length === 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
2022-08-18 02:13:22 +02:00
|
|
|
(async () => {
|
2024-09-12 00:01:12 +02:00
|
|
|
let exit_code = 0;
|
|
|
|
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const file of files) {
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
namespace.start();
|
2020-12-01 00:42:45 +01:00
|
|
|
namespace.set_global("window", window);
|
2021-10-14 00:23:21 +02:00
|
|
|
namespace.set_global("location", dom.window.location);
|
|
|
|
window.location.href = "http://zulip.zulipdev.com/#";
|
2020-12-01 00:02:16 +01:00
|
|
|
namespace.set_global("setTimeout", noop);
|
|
|
|
namespace.set_global("setInterval", noop);
|
2022-04-10 02:16:59 +02:00
|
|
|
namespace.set_global("localStorage", localStorage);
|
|
|
|
ls_container.clear();
|
2018-05-14 21:37:36 +02:00
|
|
|
_.throttle = immediate;
|
|
|
|
_.debounce = immediate;
|
2023-03-18 19:01:20 +01:00
|
|
|
zpage_billing_params.reset();
|
2021-03-25 22:35:45 +01:00
|
|
|
zpage_params.reset();
|
2018-05-14 21:37:36 +02:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
namespace.mock_esm("../../src/blueslip", blueslip);
|
|
|
|
require("../../src/blueslip");
|
|
|
|
namespace.mock_esm("../../src/i18n", stub_i18n);
|
|
|
|
require("../../src/i18n");
|
2024-02-16 22:56:36 +01:00
|
|
|
namespace.mock_esm("../../src/base_page_params", zpage_params);
|
|
|
|
require("../../src/base_page_params");
|
2023-03-18 19:01:20 +01:00
|
|
|
namespace.mock_esm("../../src/billing/page_params", zpage_billing_params);
|
|
|
|
require("../../src/billing/page_params");
|
2023-02-22 23:04:10 +01:00
|
|
|
namespace.mock_esm("../../src/page_params", zpage_params);
|
|
|
|
require("../../src/page_params");
|
2024-02-13 02:08:16 +01:00
|
|
|
namespace.mock_esm("../../src/state_data", zpage_params);
|
|
|
|
require("../../src/state_data");
|
2023-02-22 23:04:10 +01:00
|
|
|
namespace.mock_esm("../../src/user_settings", zpage_params);
|
|
|
|
require("../../src/user_settings");
|
|
|
|
namespace.mock_esm("../../src/realm_user_settings_defaults", zpage_params);
|
|
|
|
require("../../src/realm_user_settings_defaults");
|
2020-04-03 18:08:38 +02:00
|
|
|
|
2024-09-12 00:01:12 +02:00
|
|
|
try {
|
|
|
|
await run_one_module(file);
|
|
|
|
} catch (error) /* istanbul ignore next */ {
|
|
|
|
console.error(error);
|
|
|
|
exit_code = 1;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
namespace.finish();
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2024-09-12 00:01:12 +02:00
|
|
|
|
|
|
|
process.exit(exit_code);
|
2022-08-18 02:13:22 +02:00
|
|
|
})().catch((error) => /* istanbul ignore next */ {
|
2023-02-24 20:46:13 +01:00
|
|
|
console.error(error);
|
2018-05-14 21:37:36 +02:00
|
|
|
process.exit(1);
|
2022-08-18 02:13:22 +02:00
|
|
|
});
|