forked from mochajs/mocha
/
mocha.spec.js
243 lines (212 loc) · 7.5 KB
/
mocha.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
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
'use strict';
var utils = require('../../lib/utils');
var Mocha = require('../../lib/mocha');
var sinon = require('sinon');
describe('Mocha', function() {
var opts = {reporter: function() {}}; // no output
var sandbox;
beforeEach(function() {
sandbox = sinon.createSandbox();
});
afterEach(function() {
sandbox.restore();
});
describe('constructor', function() {
beforeEach(function() {
sandbox.stub(Mocha.prototype, 'useColors').returnsThis();
sandbox.stub(utils, 'deprecate');
});
it('should prefer "color" over "useColors"', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true, color: false});
expect(Mocha.prototype.useColors, 'to have a call satisfying', [false]);
});
it('should assign "useColors" to "color"', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true});
expect(Mocha.prototype.useColors, 'to have a call satisfying', [true]);
});
it('should call utils.deprecate()', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true});
expect(utils.deprecate, 'was called');
});
});
describe('.run(fn)', function() {
it('should not raise errors if callback was not provided', function() {
sandbox.stub(Mocha.Runner.prototype, 'run');
var mocha = new Mocha(opts);
expect(function() {
mocha.run();
}, 'not to throw');
});
it('should execute the callback when complete', function(done) {
var mocha = new Mocha(opts);
sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0);
mocha.run(done);
});
});
describe('.reporter("xunit").run(fn)', function() {
it('should not raise errors if callback was not provided', function() {
var mocha = new Mocha();
expect(function() {
try {
mocha.reporter('xunit').run();
} catch (e) {
console.log(e);
expect.fail(e.message);
}
}, 'not to throw');
});
});
describe('.addFile()', function() {
it('should add the given file to the files array', function() {
var mocha = new Mocha(opts);
mocha.addFile('myFile.js');
expect(mocha.files, 'to have length', 1).and('to contain', 'myFile.js');
});
});
describe('.invert()', function() {
it('should set the invert option to true', function() {
var mocha = new Mocha(opts);
mocha.invert();
expect(mocha.options, 'to have property', 'invert', true);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.invert(), 'to be', mocha);
});
});
describe('.ignoreLeaks()', function() {
it('should set the ignoreLeaks option to true when param equals true', function() {
var mocha = new Mocha(opts);
mocha.ignoreLeaks(true);
expect(mocha.options, 'to have property', 'ignoreLeaks', true);
});
it('should set the ignoreLeaks option to false when param equals false', function() {
var mocha = new Mocha(opts);
mocha.ignoreLeaks(false);
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
});
it('should set the ignoreLeaks option to false when the param is undefined', function() {
var mocha = new Mocha(opts);
mocha.ignoreLeaks();
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.ignoreLeaks(), 'to be', mocha);
});
});
describe('.checkLeaks()', function() {
it('should set the ignoreLeaks option to false', function() {
var mocha = new Mocha(opts);
mocha.checkLeaks();
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.checkLeaks(), 'to be', mocha);
});
});
describe('.fullTrace()', function() {
it('should set the fullStackTrace option to true', function() {
var mocha = new Mocha(opts);
mocha.fullTrace();
expect(mocha.options, 'to have property', 'fullStackTrace', true);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.fullTrace(), 'to be', mocha);
});
});
describe('.growl()', function() {
describe('if capable of notifications', function() {
it('should set the growl option to true', function() {
var mocha = new Mocha(opts);
mocha.isGrowlCapable = function forceEnable() {
return true;
};
mocha.growl();
expect(mocha.options, 'to have property', 'growl', true);
});
});
describe('if not capable of notifications', function() {
it('should set the growl option to false', function() {
var mocha = new Mocha(opts);
mocha.isGrowlCapable = function forceDisable() {
return false;
};
mocha.growl();
expect(mocha.options, 'to have property', 'growl', false);
});
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.growl(), 'to be', mocha);
});
});
describe('.useInlineDiffs()', function() {
it('should set the useInlineDiffs option to true when param equals true', function() {
var mocha = new Mocha(opts);
mocha.useInlineDiffs(true);
expect(mocha.options, 'to have property', 'useInlineDiffs', true);
});
it('should set the useInlineDiffs option to false when param equals false', function() {
var mocha = new Mocha(opts);
mocha.useInlineDiffs(false);
expect(mocha.options, 'to have property', 'useInlineDiffs', false);
});
it('should set the useInlineDiffs option to false when the param is undefined', function() {
var mocha = new Mocha(opts);
mocha.useInlineDiffs();
expect(mocha.options, 'to have property', 'useInlineDiffs', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.useInlineDiffs(), 'to be', mocha);
});
});
describe('.noHighlighting()', function() {
it('should set the noHighlighting option to true', function() {
var mocha = new Mocha(opts);
mocha.noHighlighting();
expect(mocha.options, 'to have property', 'noHighlighting', true);
});
});
describe('.allowUncaught()', function() {
it('should set the allowUncaught option to true', function() {
var mocha = new Mocha(opts);
mocha.allowUncaught();
expect(mocha.options, 'to have property', 'allowUncaught', true);
});
});
describe('.delay()', function() {
it('should set the delay option to true', function() {
var mocha = new Mocha(opts);
mocha.delay();
expect(mocha.options, 'to have property', 'delay', true);
});
});
describe('.bail()', function() {
it('should set the suite._bail to true if there is no arguments', function() {
var mocha = new Mocha(opts);
mocha.bail();
expect(mocha.suite._bail, 'to be', true);
});
});
describe('error handling', function() {
it('should throw reporter error if an invalid reporter is given', function() {
var updatedOpts = {reporter: 'invalidReporter', reporterOptions: {}};
var throwError = function() {
// eslint-disable-next-line no-new
new Mocha(updatedOpts);
};
expect(throwError, 'to throw', {
message: 'invalid reporter "invalidReporter"',
code: 'ERR_MOCHA_INVALID_REPORTER',
reporter: 'invalidReporter'
});
});
});
});