mirror of
https://github.com/zulip/zulip.git
synced 2025-11-13 18:36:36 +00:00
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.
115 lines
2.8 KiB
JavaScript
115 lines
2.8 KiB
JavaScript
"use strict";
|
|
|
|
const Module = require("module");
|
|
const path = require("path");
|
|
|
|
require("css.escape");
|
|
const Handlebars = require("handlebars/runtime");
|
|
const _ = require("lodash");
|
|
|
|
const handlebars = require("./handlebars");
|
|
const stub_i18n = require("./i18n");
|
|
const namespace = require("./namespace");
|
|
const test = require("./test");
|
|
const {make_zblueslip} = require("./zblueslip");
|
|
|
|
global.$ = require("./zjquery");
|
|
|
|
require("@babel/register")({
|
|
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
|
|
only: [
|
|
new RegExp("^" + _.escapeRegExp(path.resolve(__dirname, "../../static/js") + path.sep)),
|
|
new RegExp(
|
|
"^" + _.escapeRegExp(path.resolve(__dirname, "../../static/shared/js") + path.sep),
|
|
),
|
|
],
|
|
plugins: [
|
|
"babel-plugin-rewire-ts",
|
|
["@babel/plugin-transform-modules-commonjs", {lazy: () => true}],
|
|
],
|
|
});
|
|
|
|
// Create a helper function to avoid sneaky delays in tests.
|
|
function immediate(f) {
|
|
return () => f();
|
|
}
|
|
|
|
// Find the files we need to run.
|
|
const files = process.argv.slice(2);
|
|
if (files.length === 0) {
|
|
throw new Error("No tests found");
|
|
}
|
|
|
|
// Set up our namespace helpers.
|
|
const window = new Proxy(global, {
|
|
set: (obj, prop, value) => {
|
|
namespace.set_global(prop, value);
|
|
return true;
|
|
},
|
|
});
|
|
|
|
// Set up Handlebars
|
|
handlebars.hook_require();
|
|
|
|
const noop = function () {};
|
|
|
|
// Set up fake module.hot
|
|
Module.prototype.hot = {
|
|
accept: noop,
|
|
};
|
|
|
|
function short_tb(tb) {
|
|
const lines = tb.split("\n");
|
|
|
|
const i = lines.findIndex((line) => line.includes("Module._compile"));
|
|
|
|
if (i === -1) {
|
|
return tb;
|
|
}
|
|
|
|
return lines.splice(0, i + 1).join("\n") + "\n(...)\n";
|
|
}
|
|
|
|
function run_one_module(file) {
|
|
global.$.clear_all_elements();
|
|
console.info("running test " + path.basename(file, ".js"));
|
|
test.set_current_file_name(file);
|
|
require(file);
|
|
}
|
|
|
|
test.set_verbose(files.length === 1);
|
|
|
|
try {
|
|
for (const file of files) {
|
|
namespace.start();
|
|
namespace.set_global("window", window);
|
|
namespace.set_global("to_$", () => window);
|
|
namespace.set_global("location", {
|
|
hash: "#",
|
|
});
|
|
namespace.set_global("setTimeout", noop);
|
|
namespace.set_global("setInterval", noop);
|
|
_.throttle = immediate;
|
|
_.debounce = immediate;
|
|
|
|
const blueslip = namespace.set_global("blueslip", make_zblueslip());
|
|
namespace.set_global("i18n", stub_i18n);
|
|
|
|
run_one_module(file);
|
|
|
|
if (blueslip.reset) {
|
|
blueslip.reset();
|
|
}
|
|
|
|
namespace.finish();
|
|
Handlebars.HandlebarsEnvironment.call(Handlebars);
|
|
}
|
|
} catch (error) {
|
|
if (error.stack) {
|
|
console.info(short_tb(error.stack));
|
|
} else {
|
|
console.info(error);
|
|
}
|
|
process.exit(1);
|
|
}
|