gristlabs_grist-core/test/report-why-tests-hang.js
Paul Fitzpatrick bcbf57d590 (core) bump mocha version to allow parallel tests; move more tests to core
Summary:
This uses a newer version of mocha in grist-core so that tests can be run in parallel. That allows more tests to be moved without slowing things down overall. Tests moved are venerable browser tests; only the ones that "just work" or worked without too much trouble to are moved, in order to keep the diff from growing too large. Will wrestle with more in follow up.

Parallelism is at the file level, rather than the individual test.

The newer version of mocha isn't needed for grist-saas repo; tests are parallelized in our internal CI by other means. I've chosen to allocate files to workers in a cruder way than our internal CI, based on initial characters rather than an automated process. The automated process would need some reworking to be compatible with mocha running in parallel mode.

Test Plan: this diff was tested first on grist-core, then ported to grist-saas so saas repo history will correctly track history of moved files.

Reviewers: jarek

Reviewed By: jarek

Subscribers: jarek

Differential Revision: https://phab.getgrist.com/D3927
2023-06-27 02:55:34 -04:00

40 lines
1.7 KiB
JavaScript

/**
* Mocha 4 no longer forces exit of a process after tests end, so if a setTimeout() or anything
* else is keeping node running, tests will hang after finishing.
*
* This helper module, always included via mocha.opts, ensures we print something if that happens.
* We use why-is-node-running module to print something informative.
*/
// --no-exit|-E flag is interpreted by mocha-webdriver library to start REPL on failure, and we
// do NOT want to output a big dump about that.
const noexit = process.argv.includes("--no-exit") || process.argv.includes('-E');
// Don't load why-is-node-running if we're not going to use it. It probably means that we're
// in a debugging session, and this module creates async hooks that interfere with debugging.
const whyIsNodeRunning = noexit ? null : require('why-is-node-running');
function report() {
whyIsNodeRunning?.();
console.warn("*******************************************************");
console.warn("Something above prevented node from exiting on its own.");
console.warn("*******************************************************");
// We want to exit, but process.exit(1) doesn't work, since mocha catches it and insists on
// exiting with the test status result (which may be 0, and we need to indicate failure).
process.kill(process.pid, 'SIGTERM');
}
if (process.env.MOCHA_WORKER_ID === undefined) {
exports.mochaHooks = {
afterAll(done) {
if (noexit) {
console.log("report-why-tests-hang silenced with --no-exit flag");
} else {
// If still hanging after 5s after tests finish, say something. Unref() ensures that THIS
// timeout doesn't itself keep node from exiting.
setTimeout(report, 5000).unref();
}
done();
}
}
}