forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
245 lines (211 loc) · 7.42 KB
/
runner.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var mocha = require('../')
, Suite = mocha.Suite
, Runner = mocha.Runner
, Test = mocha.Test;
describe('Runner', function(){
var suite, runner;
beforeEach(function(){
suite = new Suite(null, 'root');
runner = new Runner(suite);
})
describe('.grep()', function(){
it('should update the runner.total with number of matched tests', function(){
suite.addTest(new Test('im a test about lions'));
suite.addTest(new Test('im another test about lions'));
suite.addTest(new Test('im a test about bears'));
var newRunner = new Runner(suite);
newRunner.grep(/lions/);
newRunner.total.should.equal(2);
})
it('should update the runner.total with number of matched tests when inverted', function(){
suite.addTest(new Test('im a test about lions'));
suite.addTest(new Test('im another test about lions'));
suite.addTest(new Test('im a test about bears'));
var newRunner = new Runner(suite);
newRunner.grep(/lions/, true);
newRunner.total.should.equal(1);
})
})
describe('.grepTotal()', function(){
it('should return the total number of matched tests', function(){
suite.addTest(new Test('im a test about lions'));
suite.addTest(new Test('im another test about lions'));
suite.addTest(new Test('im a test about bears'));
runner.grep(/lions/);
runner.grepTotal(suite).should.equal(2);
})
it('should return the total number of matched tests when inverted', function(){
suite.addTest(new Test('im a test about lions'));
suite.addTest(new Test('im another test about lions'));
suite.addTest(new Test('im a test about bears'));
runner.grep(/lions/, true);
runner.grepTotal(suite).should.equal(1);
})
})
describe('.globalProps()', function(){
it('should include common non enumerable globals', function() {
var props = runner.globalProps();
props.should.containEql('setTimeout');
props.should.containEql('clearTimeout');
props.should.containEql('setInterval');
props.should.containEql('clearInterval');
props.should.containEql('Date');
props.should.containEql('XMLHttpRequest');
});
});
describe('.globals()', function(){
it('should default to the known globals', function(){
runner.globals().length.should.be.above(16);
})
it('should white-list globals', function(){
runner.globals(['foo', 'bar']);
runner.globals().should.containEql('foo');
runner.globals().should.containEql('bar');
})
})
describe('.checkGlobals(test)', function(){
it('should allow variables that match a wildcard', function(done) {
runner.globals(['foo*', 'giz*']);
global.foo = 'baz';
global.gizmo = 'quux';
runner.checkGlobals();
delete global.foo;
delete global.gizmo;
done()
})
it('should emit "fail" when a new global is introduced', function(done){
runner.checkGlobals();
global.foo = 'bar';
runner.on('fail', function(test, err){
test.should.equal('im a test');
err.message.should.equal('global leak detected: foo');
delete global.foo;
done();
});
runner.checkGlobals('im a test');
})
it('should emit "fail" when a single new disallowed global is introduced after a single extra global is allowed', function(done) {
var doneCalled = false;
runner.globals('good');
global.bad = 1;
runner.on('fail', function(test, err) {
delete global.bad;
done();
doneCalled = true;
});
runner.checkGlobals('test');
if (!doneCalled) {
done(Error("Expected test failure did not occur."));
}
});
it ('should not fail when a new common global is introduced', function(){
// verify that the prop isn't enumerable
delete global.XMLHttpRequest;
global.propertyIsEnumerable('XMLHttpRequest').should.not.be.ok;
// create a new runner and keep a reference to the test.
var test = new Test('im a test about bears');
suite.addTest(test);
var newRunner = new Runner(suite);
// make the prop enumerable again.
global.XMLHttpRequest = function() {};
global.propertyIsEnumerable('XMLHttpRequest').should.be.ok;
// verify the test hasn't failed.
newRunner.checkGlobals(test);
test.should.not.have.key('state');
// clean up our global space.
delete global.XMLHttpRequest;
});
it('should pluralize the error message when several are introduced', function(done){
runner.checkGlobals();
global.foo = 'bar';
global.bar = 'baz';
runner.on('fail', function(test, err){
test.should.equal('im a test');
err.message.should.equal('global leaks detected: foo, bar');
delete global.foo;
delete global.bar;
done();
});
runner.checkGlobals('im a test');
})
it('should respect per test whitelisted globals', function() {
var test = new Test('im a test about lions');
test.globals(['foo']);
suite.addTest(test);
var runner = new Runner(suite);
global.foo = 'bar';
// verify the test hasn't failed.
runner.checkGlobals(test);
test.should.not.have.key('state');
delete global.foo;
})
it('should respect per test whitelisted globals but still detect other leaks', function(done) {
var test = new Test('im a test about lions');
test.globals(['foo']);
suite.addTest(test);
global.foo = 'bar';
global.bar = 'baz';
runner.on('fail', function(test, err){
test.title.should.equal('im a test about lions');
err.message.should.equal('global leak detected: bar');
delete global.foo;
done();
});
runner.checkGlobals(test);
})
})
describe('.fail(test, err)', function(){
it('should increment .failures', function(){
runner.failures.should.equal(0);
runner.fail({}, {});
runner.failures.should.equal(1);
runner.fail({}, {});
runner.failures.should.equal(2);
})
it('should set test.state to "failed"', function(){
var test = {};
runner.fail(test, 'some error');
test.state.should.equal('failed');
})
it('should emit "fail"', function(done){
var test = {}, err = {};
runner.on('fail', function(test, err){
test.should.equal(test);
err.should.equal(err);
done();
});
runner.fail(test, err);
})
})
describe('.failHook(hook, err)', function(){
it('should increment .failures', function(){
runner.failures.should.equal(0);
runner.failHook({}, {});
runner.failures.should.equal(1);
runner.failHook({}, {});
runner.failures.should.equal(2);
})
it('should emit "fail"', function(done){
var hook = {}, err = {};
runner.on('fail', function(hook, err){
hook.should.equal(hook);
err.should.equal(err);
done();
});
runner.failHook(hook, err);
})
it('should emit "end" if suite bail is true', function(done){
var hook = {}, err = {};
suite.bail(true);
runner.on('end', done);
runner.failHook(hook, err);
})
it('should not emit "end" if suite bail is not true', function(done){
var hook = {}, err = {};
suite.bail(false);
runner.on('end', function() { throw new Error('"end" was emit, but the bail is false'); });
runner.failHook(hook, err);
done();
})
})
})