-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
exit.spec.js
80 lines (68 loc) · 2.01 KB
/
exit.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
var runMocha = require('../helpers').runMocha;
describe('--exit', function() {
var behaviors = {
enabled: '--exit',
disabled: '--no-exit'
};
// subprocess
var mocha;
function killSubprocess() {
mocha.kill('SIGKILL');
}
// these two handlers deal with a ctrl-c on command-line
before(function() {
process.on('SIGINT', killSubprocess);
});
after(function() {
process.removeListener('SIGINT', killSubprocess);
});
/**
* Returns a test that executes Mocha in a subprocess with either
* `--exit`, `--no-exit`, or default behavior.
*
* @param {boolean} shouldExit - Expected result; `true` if Mocha should
* have force-killed the process.
* @param {"enabled"|"disabled"} [behavior] - 'enabled' or 'disabled'; omit for default
* @returns {Function} async function implementing the test
*/
var runExit = function(shouldExit, behavior) {
return function(done) {
var timeout = this.timeout();
this.timeout(0);
this.slow(Infinity);
var didExit = true;
var timeoutObj;
var fixture = 'exit.fixture.js';
var args = behaviors[behavior] ? [behaviors[behavior]] : [];
mocha = runMocha(fixture, args, function postmortem(err) {
clearTimeout(timeoutObj);
if (err) {
return done(err);
}
expect(didExit, 'to be', shouldExit);
done();
});
// If this callback happens, then Mocha didn't automatically exit.
timeoutObj = setTimeout(function() {
didExit = false;
killSubprocess();
}, timeout - 500);
};
};
describe('default behavior', function() {
it('should not force exit after root suite completion', runExit(false));
});
describe('when enabled', function() {
it(
'should force exit after root suite completion',
runExit(true, 'enabled')
);
});
describe('when disabled', function() {
it(
'should not force exit after root suite completion',
runExit(false, 'disabled')
);
});
});