Files
zulip/frontend_tests/zjsunit/index.js
Anders Kaseorg 28f3dfa284 js: Automatically convert var to let and const in most files.
This commit was originally automatically generated using `tools/lint
--only=eslint --fix`.  It was then modified by tabbott to contain only
changes to a set of files that are unlikely to result in significant
merge conflicts with any open pull request, excluding about 20 files.
His plan is to merge the remaining changes with more precise care,
potentially involving merging parts of conflicting pull requests
before running the `eslint --fix` operation.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-11-03 12:42:39 -08:00

133 lines
3.3 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const escapeRegExp = require("lodash/escapeRegExp");
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');
global._ = require('underscore/underscore.js');
const _ = global._;
const windowObj = {
location: {
hash: '#',
},
};
global.window = _.extend({}, windowObj, {
to_$: () => {
return windowObj;
},
});
global.Dict = require('../../static/js/dict').Dict;
// Create a helper function to avoid sneaky delays in tests.
function immediate(f) {
return () => {
return f();
};
}
// Find the files we need to run.
const finder = require('./finder.js');
const files = finder.find_files_to_run(); // may write to console
if (_.isEmpty(files)) {
throw "No tests found";
}
// Set up our namespace helpers.
const namespace = require('./namespace.js');
global.set_global = namespace.set_global;
global.patch_builtin = namespace.patch_builtin;
global.zrequire = namespace.zrequire;
global.stub_out_jquery = namespace.stub_out_jquery;
global.with_overrides = namespace.with_overrides;
// Set up stub helpers.
const stub = require('./stub.js');
global.with_stub = stub.with_stub;
// Set up fake jQuery
global.make_zjquery = require('./zjquery.js').make_zjquery;
// Set up fake blueslip
global.make_zblueslip = require('./zblueslip.js').make_zblueslip;
// Set up fake translation
global.stub_i18n = require('./i18n.js');
// Set up Handlebars
const handlebars = require('./handlebars.js');
global.make_handlebars = handlebars.make_handlebars;
global.stub_templates = handlebars.stub_templates;
const noop = function () {};
// Set up fake module.hot
// eslint-disable-next-line no-native-reassign
module = require('module');
module.prototype.hot = {
accept: noop,
};
// 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'));
return data;
};
function short_tb(tb) {
const lines = tb.split('\n');
const i = _.findIndex(lines, (line) => {
return 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 bugdown comparison helper
global.bugdown_assert = require('./bugdown_assert.js');
function run_one_module(file) {
console.info('running tests for ' + file.name);
require(file.full_name);
}
global.run_test = (label, f) => {
if (files.length === 1) {
console.info(' test: ' + label);
}
f();
};
try {
files.forEach(function (file) {
global.patch_builtin('setTimeout', noop);
global.patch_builtin('setInterval', noop);
_.throttle = immediate;
_.debounce = immediate;
run_one_module(file);
namespace.restore();
});
} catch (e) {
if (e.stack) {
console.info(short_tb(e.stack));
} else {
console.info(e);
}
process.exit(1);
}