zulip/frontend_tests/zjsunit/index.js

150 lines
3.8 KiB
JavaScript
Raw Normal View History

const fs = require("fs");
const Module = require("module");
const path = require("path");
const escapeRegExp = require("lodash/escapeRegExp");
2018-04-12 22:50:09 +02:00
const finder = require("./finder.js");
const handlebars = require("./handlebars.js");
const stub_i18n = require("./i18n.js");
const namespace = require("./namespace.js");
const stub = require("./stub.js");
const make_blueslip = require("./zblueslip.js").make_zblueslip;
const zjquery = require("./zjquery.js");
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: ["rewire-ts"],
});
global.assert = require("assert").strict;
global._ = require("underscore/underscore.js");
const _ = global._;
// Create a helper function to avoid sneaky delays in tests.
function immediate(f) {
return () => f();
}
// Find the files we need to run.
const files = finder.find_files_to_run(); // may write to console
if (files.length === 0) {
throw "No tests found";
}
2016-07-30 17:00:12 +02:00
// Set up our namespace helpers.
global.set_global = namespace.set_global;
global.patch_builtin = namespace.set_global;
global.zrequire = namespace.zrequire;
2016-07-30 20:01:15 +02:00
global.stub_out_jquery = namespace.stub_out_jquery;
global.with_overrides = namespace.with_overrides;
2016-07-30 17:00:12 +02:00
global.window = new Proxy(global, {
set: (obj, prop, value) => namespace.set_global(prop, value),
});
global.to_$ = () => window;
2017-03-11 19:45:10 +01:00
// Set up stub helpers.
global.make_stub = stub.make_stub;
2017-03-11 19:45:10 +01:00
global.with_stub = stub.with_stub;
// Set up fake jQuery
global.make_zjquery = zjquery.make_zjquery;
2017-06-28 10:16:34 +02:00
// Set up Handlebars
global.make_handlebars = handlebars.make_handlebars;
global.stub_templates = handlebars.stub_templates;
const noop = function () {};
// Set up fake module.hot
Module.prototype.hot = {
accept: noop,
};
2018-04-12 22:50:09 +02:00
// Set up fixtures.
global.read_fixture_data = (fn) => {
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;
};
function short_tb(tb) {
const lines = tb.split("\n");
const i = lines.findIndex(
(line) => line.includes("run_test") || line.includes("run_one_module"),
);
if (i === -1) {
return tb;
}
return lines.splice(0, i + 1).join("\n") + "\n(...)\n";
}
// Set up markdown comparison helper
global.markdown_assert = require("./markdown_assert.js");
let current_file_name;
function run_one_module(file) {
console.info("running tests for " + file.name);
current_file_name = file.name;
require(file.full_name);
}
global.run_test = (label, f) => {
if (files.length === 1) {
console.info(" test: " + label);
}
try {
global.with_overrides(f);
} catch (error) {
console.info("-".repeat(50));
console.info(`test failed: ${current_file_name} > ${label}`);
console.info();
throw error;
}
// defensively reset blueslip after each test.
blueslip.reset();
};
try {
files.forEach((file) => {
set_global("location", {
hash: "#",
});
global.patch_builtin("setTimeout", noop);
global.patch_builtin("setInterval", noop);
_.throttle = immediate;
_.debounce = immediate;
set_global("blueslip", make_blueslip());
set_global("i18n", stub_i18n);
namespace.clear_zulip_refs();
run_one_module(file);
if (blueslip.reset) {
blueslip.reset();
}
namespace.restore();
});
} catch (e) {
if (e.stack) {
console.info(short_tb(e.stack));
} else {
console.info(e);
}
process.exit(1);
}