Skip to content

Commit c20c556

Browse files
author
James Halliday
committed
use a setInterval to keep the event loop alive
1 parent e7d00be commit c20c556

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

index.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ var createDefaultStream = require('./lib/default_stream');
22
var Render = require('./lib/render');
33
var Test = require('./lib/test');
44

5-
exports = module.exports = createHarness();
6-
exports.createHarness = createHarness;
7-
exports.Test = Test;
8-
95
var canEmitExit = typeof process !== 'undefined' && process
106
&& typeof process.on === 'function'
117
;
@@ -20,6 +16,10 @@ var onexit = (function () {
2016
return function (cb) { stack.push(cb) };
2117
})();
2218

19+
exports = module.exports = createHarness();
20+
exports.createHarness = createHarness;
21+
exports.Test = Test;
22+
2323
function createHarness (conf_) {
2424
var pending = [];
2525
var running = false;
@@ -29,11 +29,30 @@ function createHarness (conf_) {
2929
var only = false;
3030
var closed = false;
3131
var out = new Render();
32+
if (!conf_) conf_ = {};
33+
34+
var tests = [];
35+
var exitInterval = conf_.exitInterval !== false && canEmitExit
36+
&& typeof process._getActiveHandles === 'function'
37+
&& setInterval(function () {
38+
if (process._getActiveHandles().length === 1) {
39+
tests.forEach(function (t) { t._exit() });
40+
}
41+
}, 200);
42+
43+
var exitCode = 0;
44+
var exit = function (c) { exitCode = c };
45+
46+
out.on('end', function () {
47+
clearInterval(exitInterval);
48+
process.exit(exitCode);
49+
});
3250

3351
var test = function (name, conf, cb) {
3452
count++;
3553
var t = new Test(name, conf, cb);
36-
if (!conf || typeof conf !== 'object') conf = conf_ || {};
54+
tests.push(t);
55+
if (!conf || typeof conf !== 'object') conf = conf_;
3756

3857
if (conf.exit !== false) {
3958
onexit(function (code) {
@@ -43,7 +62,7 @@ function createHarness (conf_) {
4362
out.close();
4463
}
4564
if (!code && !t._ok && (!only || name === only)) {
46-
process.exit(1);
65+
exit(1);
4766
}
4867
});
4968
}
@@ -101,7 +120,7 @@ function createHarness (conf_) {
101120
out.close();
102121
}
103122
if (conf.exit !== false && canExit && !t._ok) {
104-
process.exit(1);
123+
exit(1);
105124
}
106125
});
107126
}

0 commit comments

Comments
 (0)