forked from mochajs/mocha
/
multiple-done.spec.js
157 lines (139 loc) · 4.92 KB
/
multiple-done.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
var runMochaJSON = require('./helpers').runMochaJSON;
var invokeMocha = require('./helpers').invokeMocha;
var MULTIPLE_DONE = require('../../lib/errors').constants.MULTIPLE_DONE;
describe('multiple calls to done()', function() {
var res;
describe('from a spec', function() {
before(function(done) {
runMochaJSON('multiple-done', function(err, result) {
res = result;
done(err);
});
});
it('results in failure', function() {
expect(res, 'to have failed test count', 1)
.and('to have passed test count', 1)
.and('to have pending test count', 0)
.and('to have failed');
});
it('throws a descriptive error', function() {
expect(res, 'to have failed with error', {
message: /done\(\) called multiple times in test <should fail in a test-case> \(of root suite\) of file.+multiple-done\.fixture\.js/,
code: MULTIPLE_DONE
});
});
});
describe('with error passed on second call', function() {
before(function(done) {
runMochaJSON('multiple-done-with-error', function(err, result) {
res = result;
done(err);
});
});
it('results in failure', function() {
expect(res, 'to have failed test count', 1)
.and('to have passed test count', 1)
.and('to have pending test count', 0)
.and('to have failed');
});
it('should throw a descriptive error', function() {
expect(res, 'to have failed with error', {
message: /done\(\) called multiple times in test <should fail in a test-case> \(of root suite\) of file.+multiple-done-with-error\.fixture\.js; in addition, done\(\) received error: Error: second error/,
code: MULTIPLE_DONE
});
});
});
describe('with multiple specs', function() {
before(function(done) {
runMochaJSON('multiple-done-specs', function(err, result) {
res = result;
done(err);
});
});
it('results in failure', function() {
expect(res, 'to have failed test count', 1)
.and('to have passed test count', 2)
.and('to have pending test count', 0)
.and('to have failed');
});
it('correctly attributes the error', function() {
expect(res.failures[0], 'to satisfy', {
fullTitle: 'suite test1',
err: {
message: /done\(\) called multiple times in test <suite test1> of file.+multiple-done-specs\.fixture\.js/,
code: MULTIPLE_DONE
}
});
});
});
describe('from a before hook', function() {
before(function(done) {
runMochaJSON('multiple-done-before', function(err, result) {
res = result;
done(err);
});
});
it('results in failure', function() {
expect(res, 'to have failed test count', 1)
.and('to have passed test count', 1)
.and('to have pending test count', 0)
.and('to have failed');
});
it('correctly attributes the error', function() {
expect(res.failures[0], 'to satisfy', {
fullTitle: 'suite1 "before all" hook in "suite1"',
err: {
message: /done\(\) called multiple times in hook <suite1 "before all" hook in "suite1"> of file.+multiple-done-before\.fixture\.js/
}
});
});
});
describe('from a "before each" hook', function() {
before(function(done) {
runMochaJSON('multiple-done-before-each', function(err, result) {
res = result;
done(err);
});
});
it('results in a failure', function() {
expect(res, 'to have failed test count', 2)
.and('to have passed test count', 2)
.and('to have pending test count', 0)
.and('to have exit code', 2);
});
it('correctly attributes the errors', function() {
expect(res.failures[0], 'to equal', res.failures[1]).and('to satisfy', {
fullTitle: 'suite1 "before each" hook in "suite1"',
err: {
message: /done\(\) called multiple times in hook <suite1 "before each" hook in "suite1"> of file.+multiple-done-before-each\.fixture\.js/,
multiple: [
{
code: 'ERR_MOCHA_MULTIPLE_DONE'
}
]
}
});
});
});
describe('when done() called asynchronously', function() {
before(function(done) {
// we can't be sure that mocha won't fail with an uncaught exception here, which would cause any JSON
// output to be befouled; we need to run "raw" and capture STDERR
invokeMocha(
require.resolve('./fixtures/multiple-done-async.fixture.js'),
function(err, result) {
res = result;
done(err);
},
'pipe'
);
});
it('results in error', function() {
expect(res, 'to satisfy', {
code: expect.it('to be greater than', 0),
output: /done\(\) called multiple times in test <should fail in an async test case> \(of root suite\) of file.+multiple-done-async\.fixture\.js/
});
});
});
});