forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
188 lines (164 loc) · 5.71 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
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.include('setTimeout');
props.should.include('clearTimeout');
props.should.include('setInterval');
props.should.include('clearInterval');
props.should.include('Date');
props.should.include('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.include('foo');
runner.globals().should.include('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 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');
})
})
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(hoot, 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"', function(done){
var hook = {}, err = {};
runner.on('end', done);
runner.failHook(hook, err);
})
})
})