Skip to content

Commit

Permalink
Issue mochajs#3817 prevent idle event loop during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rlibm committed Mar 10, 2019
1 parent ca9eba6 commit bd4a611
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 57 deletions.
18 changes: 18 additions & 0 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,25 @@ Mocha.prototype.run = function(fn) {
exports.reporters.Base.inlineDiffs = options.useInlineDiffs;
exports.reporters.Base.hideDiff = options.hideDiff;

// We force a timeout to exist that "squats" on the event loop till all tests
// complete. It does nothing except maintain a presence.
// See: https://github.com/mochajs/mocha/issues/3817
var setInterval = global.setInterval;
var clearInterval = global.clearInterval;
var eventLoopHold = setInterval(utils.noop, 1000);

function clearEventLoop() {
if (!eventLoopHold) {
return;
}
clearInterval(eventLoopHold);
eventLoopHold.unref();
eventLoopHold = null;
}
runner.once('end', clearEventLoop);

function done(failures) {
clearEventLoop();
fn = fn || utils.noop;
if (reporter.done) {
reporter.done(failures, fn);
Expand Down
77 changes: 20 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ module.exports = {
description: 'Run Node.js reporter tests',
hiddenFromHelp: true
},
noTimeout: {
script: test('noTimeout', '--no-timeout "test/no-timeout/*.spec.js"'),
description: 'Run Node.js with no-timeout tests',
hiddenFromHelp: true
},
only: {
default: {
script: `nps ${[
Expand Down
8 changes: 8 additions & 0 deletions test/no-timeout/no-timeout.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

describe('To be run with no timeout', function() {
it('should run', function(done) {
// note that the unref is critical for test case
global.setTimeout(done, 50).unref();
});
});

0 comments on commit bd4a611

Please sign in to comment.