Skip to content

Commit 2ff1cb2

Browse files
authored
uncaughtException: refactor, move and add tests
1 parent b431609 commit 2ff1cb2

9 files changed

+94
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
describe("Uncaught exception after runner's end", () => {
4+
it('test', () => {
5+
setTimeout(() => {
6+
throw new Error('Unexpected crash');
7+
}, 100);
8+
});
9+
});

test/integration/regression.spec.js

-33
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ var run = require('./helpers').runMocha;
44
var runJSON = require('./helpers').runMochaJSON;
55

66
describe('regressions', function() {
7-
it('issue-1327: should run the first test and then bail', function(done) {
8-
var args = [];
9-
runJSON('regression/issue-1327.fixture.js', args, function(err, res) {
10-
if (err) {
11-
return done(err);
12-
}
13-
expect(res, 'to have failed')
14-
.and('to have passed test count', 1)
15-
.and('to have failed test count', 1)
16-
.and('to have passed test', 'test 1')
17-
.and('to have failed test', 'test 1');
18-
done();
19-
});
20-
});
21-
227
it('issue-1991: Declarations do not get cleaned up unless you set them to `null` - Memory Leak', function(done) {
238
// on a modern MBP takes ±5 seconds on node 4.0, but on older laptops with node 0.12 ±40 seconds.
249
// Could easily take longer on even weaker machines (Travis-CI containers for example).
@@ -90,22 +75,4 @@ describe('regressions', function() {
9075
done();
9176
});
9277
});
93-
94-
it('issue-1417 uncaught exceptions from async specs', function(done) {
95-
runJSON('regression/issue-1417.fixture.js', [], function(err, res) {
96-
if (err) {
97-
done(err);
98-
return;
99-
}
100-
expect(res, 'to have failed with errors', 'sync error a', 'sync error b')
101-
.and('to have exit code', 2)
102-
.and('not to have passed tests')
103-
.and('not to have pending tests')
104-
.and('to have failed test order', [
105-
'fails exactly once when a global error is thrown synchronously and done errors',
106-
'fails exactly once when a global error is thrown synchronously and done completes'
107-
]);
108-
done();
109-
});
110-
});
11178
});

test/integration/uncaught.spec.js

+85-36
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
'use strict';
22

3-
var assert = require('assert');
4-
var run = require('./helpers').runMochaJSON;
3+
var helpers = require('./helpers');
4+
var run = helpers.runMochaJSON;
5+
var runMocha = helpers.runMocha;
6+
var invokeNode = helpers.invokeNode;
57
var args = [];
68

79
describe('uncaught exceptions', function() {
810
it('handles uncaught exceptions from hooks', function(done) {
9-
run('uncaught-hook.fixture.js', args, function(err, res) {
11+
run('uncaught/hook.fixture.js', args, function(err, res) {
1012
if (err) {
11-
done(err);
12-
return;
13+
return done(err);
1314
}
14-
assert.strictEqual(res.stats.pending, 0);
15-
assert.strictEqual(res.stats.passes, 0);
16-
assert.strictEqual(res.stats.failures, 1);
17-
18-
assert.strictEqual(
19-
res.failures[0].fullTitle,
20-
'uncaught "before each" hook for "test"'
21-
);
22-
assert.strictEqual(res.code, 1);
15+
16+
expect(res, 'to have failed with error', 'oh noes')
17+
.and('to have passed test count', 0)
18+
.and('to have pending test count', 0)
19+
.and('to have failed test count', 1)
20+
.and('to have failed test', '"before each" hook for "test"');
21+
2322
done();
2423
});
2524
});
2625

2726
it('handles uncaught exceptions from async specs', function(done) {
28-
run('uncaught.fixture.js', args, function(err, res) {
27+
run('uncaught/double.fixture.js', args, function(err, res) {
2928
if (err) {
30-
done(err);
31-
return;
29+
return done(err);
3230
}
33-
assert.strictEqual(res.stats.pending, 0);
34-
assert.strictEqual(res.stats.passes, 0);
35-
assert.strictEqual(res.stats.failures, 2);
36-
37-
assert.strictEqual(
38-
res.failures[0].title,
39-
'fails exactly once when a global error is thrown first'
40-
);
41-
assert.strictEqual(
42-
res.failures[1].title,
43-
'fails exactly once when a global error is thrown second'
44-
);
45-
assert.strictEqual(res.code, 2);
31+
32+
expect(res, 'to have failed with error', 'global error', 'test error')
33+
.and('to have passed test count', 0)
34+
.and('to have pending test count', 0)
35+
.and('to have failed test count', 2)
36+
.and(
37+
'to have failed test',
38+
'fails exactly once when a global error is thrown first',
39+
'fails exactly once when a global error is thrown second'
40+
);
41+
4642
done();
4743
});
4844
});
4945

5046
it('handles uncaught exceptions from which Mocha cannot recover', function(done) {
51-
run('uncaught-fatal.fixture.js', args, function(err, res) {
47+
run('uncaught/fatal.fixture.js', args, function(err, res) {
5248
if (err) {
5349
return done(err);
5450
}
5551

5652
var testName = 'should bail if a successful test asynchronously fails';
57-
expect(res, 'to have failed')
53+
expect(res, 'to have failed with error', 'global error')
5854
.and('to have passed test count', 1)
5955
.and('to have failed test count', 1)
6056
.and('to have passed test', testName)
@@ -65,12 +61,12 @@ describe('uncaught exceptions', function() {
6561
});
6662

6763
it('handles uncaught exceptions within pending tests', function(done) {
68-
run('uncaught-pending.fixture.js', args, function(err, res) {
64+
run('uncaught/pending.fixture.js', args, function(err, res) {
6965
if (err) {
7066
return done(err);
7167
}
7268

73-
expect(res, 'to have failed')
69+
expect(res, 'to have failed with error', 'I am uncaught!')
7470
.and('to have passed test count', 3)
7571
.and('to have pending test count', 1)
7672
.and('to have failed test count', 1)
@@ -115,13 +111,66 @@ describe('uncaught exceptions', function() {
115111
});
116112

117113
it('removes uncaught exceptions handlers correctly', function(done) {
118-
run('uncaught/listeners.fixture.js', args, function(err, res) {
114+
var path = require.resolve('./fixtures/uncaught/listeners.fixture.js');
115+
invokeNode([path], function(err, res) {
119116
if (err) {
120117
return done(err);
121118
}
122119

123-
expect(res, 'to have passed').and('to have passed test count', 0);
120+
expect(res, 'to have passed');
121+
done();
122+
});
123+
});
124124

125+
it("handles uncaught exceptions after runner's end", function(done) {
126+
runMocha(
127+
'uncaught/after-runner.fixture.js',
128+
args,
129+
function(err, res) {
130+
if (err) {
131+
return done(err);
132+
}
133+
134+
expect(res, 'to have failed').and('to satisfy', {
135+
failing: 0,
136+
passing: 1,
137+
pending: 0,
138+
output: expect.it('to contain', 'Error: Unexpected crash')
139+
});
140+
141+
done();
142+
},
143+
'pipe'
144+
);
145+
});
146+
147+
it('issue-1327: should run the first test and then bail', function(done) {
148+
run('uncaught/issue-1327.fixture.js', args, function(err, res) {
149+
if (err) {
150+
return done(err);
151+
}
152+
expect(res, 'to have failed with error', 'Too bad')
153+
.and('to have passed test count', 1)
154+
.and('to have failed test count', 1)
155+
.and('to have passed test', 'test 1')
156+
.and('to have failed test', 'test 1');
157+
done();
158+
});
159+
});
160+
161+
it('issue-1417: uncaught exceptions from async specs', function(done) {
162+
run('uncaught/issue-1417.fixture.js', args, function(err, res) {
163+
if (err) {
164+
return done(err);
165+
}
166+
expect(res, 'to have failed with errors', 'sync error a', 'sync error b')
167+
.and('to have exit code', 2)
168+
.and('not to have passed tests')
169+
.and('not to have pending tests')
170+
.and('to have failed test order', [
171+
'fails exactly once when a global error is thrown synchronously and done errors',
172+
'fails exactly once when a global error is thrown synchronously and done completes'
173+
]);
125174
done();
126175
});
127176
});

0 commit comments

Comments
 (0)