2019-11-02 00:06:25 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const escapeRegExp = require("lodash/escapeRegExp");
|
2018-04-12 22:50:09 +02:00
|
|
|
|
2019-10-12 03:37:06 +02:00
|
|
|
require("@babel/register")({
|
|
|
|
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
|
|
|
|
only: [
|
|
|
|
new RegExp("^" + escapeRegExp(path.resolve(__dirname, "../../static/js")) + path.sep),
|
2019-10-04 23:08:11 +02:00
|
|
|
new RegExp("^" + escapeRegExp(path.resolve(__dirname, "../../static/shared/js")) + path.sep),
|
2019-10-12 03:37:06 +02:00
|
|
|
],
|
|
|
|
plugins: ["rewire-ts"],
|
|
|
|
});
|
2013-08-20 06:15:41 +02:00
|
|
|
|
2020-02-12 11:49:02 +01:00
|
|
|
global.assert = require('assert').strict;
|
2019-06-21 03:16:48 +02:00
|
|
|
global._ = require('underscore/underscore.js');
|
2019-11-02 00:06:25 +01:00
|
|
|
const _ = global._;
|
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) {
|
|
|
|
return () => {
|
|
|
|
return f();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-30 18:14:42 +02:00
|
|
|
// Find the files we need to run.
|
2019-11-02 00:06:25 +01:00
|
|
|
const finder = require('./finder.js');
|
|
|
|
const files = finder.find_files_to_run(); // may write to console
|
2020-02-08 06:23:59 +01:00
|
|
|
if (files.length === 0) {
|
2016-07-30 18:14:42 +02:00
|
|
|
throw "No tests found";
|
|
|
|
}
|
|
|
|
|
2016-07-30 17:00:12 +02:00
|
|
|
// Set up our namespace helpers.
|
2019-11-02 00:06:25 +01:00
|
|
|
const namespace = require('./namespace.js');
|
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;
|
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, {
|
|
|
|
set: (obj, prop, value) => namespace.set_global(prop, value),
|
|
|
|
});
|
|
|
|
global.to_$ = () => window;
|
|
|
|
|
2017-03-11 19:45:10 +01:00
|
|
|
// Set up stub helpers.
|
2019-11-02 00:06:25 +01:00
|
|
|
const stub = require('./stub.js');
|
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
|
2017-05-25 01:49:29 +02:00
|
|
|
global.make_zjquery = require('./zjquery.js').make_zjquery;
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2018-04-09 10:01:24 +02:00
|
|
|
// Set up fake blueslip
|
2020-04-03 18:08:38 +02:00
|
|
|
const make_blueslip = require('./zblueslip.js').make_zblueslip;
|
2018-04-09 10:01:24 +02:00
|
|
|
|
2017-06-28 10:16:34 +02:00
|
|
|
// Set up fake translation
|
2020-02-27 16:56:28 +01:00
|
|
|
const stub_i18n = require('./i18n.js');
|
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
|
|
|
const handlebars = require('./handlebars.js');
|
|
|
|
global.make_handlebars = handlebars.make_handlebars;
|
|
|
|
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
|
|
|
const Module = require('module');
|
|
|
|
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) => {
|
2019-11-02 00:06:25 +01: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) {
|
|
|
|
const lines = tb.split('\n');
|
|
|
|
|
2020-02-08 05:32:25 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
return lines.splice(0, i + 1).join('\n') + '\n(...)\n';
|
2018-05-14 21:37:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 09:01:37 +01:00
|
|
|
// Set up bugdown comparison helper
|
|
|
|
global.bugdown_assert = require('./bugdown_assert.js');
|
|
|
|
|
2018-05-14 21:37:36 +02:00
|
|
|
function run_one_module(file) {
|
2016-07-30 18:14:42 +02:00
|
|
|
console.info('running tests for ' + file.name);
|
|
|
|
require(file.full_name);
|
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) {
|
|
|
|
console.info(' test: ' + label);
|
|
|
|
}
|
|
|
|
f();
|
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 {
|
|
|
|
files.forEach(function (file) {
|
2019-07-25 09:13:22 +02:00
|
|
|
set_global('location', {
|
|
|
|
hash: '#',
|
|
|
|
});
|
2018-05-14 21:37:36 +02:00
|
|
|
global.patch_builtin('setTimeout', noop);
|
|
|
|
global.patch_builtin('setInterval', noop);
|
|
|
|
_.throttle = immediate;
|
|
|
|
_.debounce = immediate;
|
|
|
|
|
2020-04-03 18:08:38 +02:00
|
|
|
set_global('blueslip', make_blueslip());
|
2020-02-27 16:56:28 +01: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();
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (e.stack) {
|
|
|
|
console.info(short_tb(e.stack));
|
|
|
|
} else {
|
|
|
|
console.info(e);
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|