forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runnable.js
216 lines (186 loc) · 6 KB
/
runnable.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var mocha = require('../')
, Runnable = mocha.Runnable
, EventEmitter = require('events').EventEmitter;
describe('Runnable(title, fn)', function(){
describe('#timeout(ms)', function(){
it('should set the timeout', function(){
var run = new Runnable;
run.timeout(1000)
run.timeout().should.equal(1000);
})
})
describe('.title', function(){
it('should be present', function(){
new Runnable('foo').title.should.equal('foo');
})
})
describe('when arity >= 1', function(){
it('should be .async', function(){
var run = new Runnable('foo', function(done){});
run.async.should.equal(1);
run.sync.should.be.false;
})
})
describe('when arity == 0', function(){
it('should be .sync', function(){
var run = new Runnable('foo', function(){});
run.async.should.be.equal(0);
run.sync.should.be.true;
})
})
describe('.run(fn)', function(){
describe('when .pending', function(){
it('should not invoke the callback', function(done){
var test = new Runnable('foo', function(){
throw new Error('should not be called');
});
test.pending = true;
test.run(done);
})
})
describe('when sync', function(){
describe('without error', function(){
it('should invoke the callback', function(done){
var calls = 0;
var test = new Runnable('foo', function(){
++calls;
});
test.run(function(err){
calls.should.equal(1);
test.duration.should.be.a('number');
done(err);
})
})
})
describe('when an exception is thrown', function(){
it('should invoke the callback', function(done){
var calls = 0;
var test = new Runnable('foo', function(){
++calls;
throw new Error('fail');
});
test.run(function(err){
calls.should.equal(1);
err.message.should.equal('fail');
done();
})
})
})
})
describe('when async', function(){
describe('without error', function(){
it('should invoke the callback', function(done){
var calls = 0;
var test = new Runnable('foo', function(done){
process.nextTick(done);
});
test.run(done);
})
})
describe('when the callback is invoked several times', function(){
describe('without an error', function(){
it('should emit a single "error" event', function(done){
var calls = 0;
var errCalls = 0;
var test = new Runnable('foo', function(done){
process.nextTick(done);
process.nextTick(done);
process.nextTick(done);
process.nextTick(done);
});
test.on('error', function(err){
++errCalls;
err.message.should.equal('done() called multiple times');
calls.should.equal(1);
errCalls.should.equal(1);
done();
});
test.run(function(){
++calls;
});
})
})
describe('with an error', function(){
it('should emit a single "error" event', function(done){
var calls = 0;
var errCalls = 0;
var test = new Runnable('foo', function(done){
done(new Error('fail'));
process.nextTick(done);
done(new Error('fail'));
process.nextTick(done);
process.nextTick(done);
});
test.on('error', function(err){
++errCalls;
err.message.should.equal('done() called multiple times');
calls.should.equal(1);
errCalls.should.equal(1);
done();
});
test.run(function(){
++calls;
});
})
})
})
describe('when an exception is thrown', function(){
it('should invoke the callback', function(done){
var calls = 0;
var test = new Runnable('foo', function(done){
throw new Error('fail');
process.nextTick(done);
});
test.run(function(err){
err.message.should.equal('fail');
done();
});
})
})
describe('when no exception is thrown async in done(fn)', function(){
it('should invoke the callback without an error', function(done){
var calls = 0;
var test = new Runnable('foo', function(done){
process.nextTick(function () {
done(function () { // provide verify fn to done
var a = 1;
return 'bar'; // this is ignored, only thrown exeptions cause fail
});
});
});
test.run(function(err){
done(err); // err should be undefined since no exceptions thrown in verify fn
});
})
})
describe('when an exception is thrown async in done(fn)', function(){
it('should invoke the callback with exception', function(done){
var calls = 0;
var test = new Runnable('foo', function(done){
process.nextTick(function () {
done(function () { // provide verify fn to done
throw new Error('fail'); // is caught and reported
});
});
});
test.run(function(err){
err.message.should.equal('fail');
done();
});
})
})
describe('when an error is passed', function(){
it('should invoke the callback', function(done){
var calls = 0;
var test = new Runnable('foo', function(done){
done(new Error('fail'));
});
test.run(function(err){
err.message.should.equal('fail');
done();
});
})
})
})
})
})