/
mocha.spec.js
359 lines (320 loc) · 11.2 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
'use strict';
const path = require('path');
const rewiremock = require('rewiremock/node');
const {createSandbox} = require('sinon');
const {EventEmitter} = require('events');
const MODULE_PATH = require.resolve('../../lib/mocha.js');
const DUMB_FIXTURE_PATH = require.resolve('./fixtures/dumb-module.fixture.js');
const DUMBER_FIXTURE_PATH = require.resolve(
'./fixtures/dumber-module.fixture.js'
);
describe('Mocha', function() {
let stubs;
let opts;
let Mocha;
let sandbox;
beforeEach(function() {
sandbox = createSandbox();
opts = {reporter: sandbox.stub()};
stubs = {};
stubs.utils = {
supportsEsModules: sandbox.stub().returns(false),
warn: sandbox.stub(),
isString: sandbox.stub(),
noop: sandbox.stub(),
cwd: sandbox.stub().returns(process.cwd()),
isBrowser: sandbox.stub().returns(false)
};
stubs.suite = Object.assign(sandbox.createStubInstance(EventEmitter), {
slow: sandbox.stub(),
timeout: sandbox.stub(),
bail: sandbox.stub(),
reset: sandbox.stub(),
dispose: sandbox.stub()
});
stubs.Suite = sandbox.stub().returns(stubs.suite);
stubs.Suite.constants = {};
stubs.BufferedRunner = sandbox.stub().returns({});
const runner = Object.assign(sandbox.createStubInstance(EventEmitter), {
run: sandbox
.stub()
.callsArgAsync(0)
.returnsThis(),
globals: sandbox.stub(),
grep: sandbox.stub(),
dispose: sandbox.stub()
});
stubs.Runner = sandbox.stub().returns(runner);
// the Runner constructor is the main export, and constants is a static prop.
// we don't need the constants themselves, but the object cannot be undefined
stubs.Runner.constants = {};
Mocha = rewiremock.proxy(MODULE_PATH, r => ({
'../../lib/utils.js': r.with(stubs.utils).callThrough(),
'../../lib/suite.js': stubs.Suite,
'../../lib/nodejs/parallel-buffered-runner.js': stubs.BufferedRunner,
'../../lib/runner.js': stubs.Runner
}));
delete require.cache[DUMB_FIXTURE_PATH];
delete require.cache[DUMBER_FIXTURE_PATH];
});
afterEach(function() {
delete require.cache[DUMB_FIXTURE_PATH];
delete require.cache[DUMBER_FIXTURE_PATH];
sandbox.restore();
});
describe('instance method', function() {
let mocha;
beforeEach(function() {
mocha = new Mocha(opts);
});
describe('parallelMode()', function() {
describe('when `Mocha` is running in Node.js', function() {
it('should return the Mocha instance', function() {
expect(mocha.parallelMode(), 'to be', mocha);
});
describe('when parallel mode is already enabled', function() {
beforeEach(function() {
mocha.options.parallel = true;
mocha._runnerClass = stubs.BufferedRunner;
mocha._lazyLoadFiles = true;
});
it('should not swap the Runner, nor change lazy loading setting', function() {
expect(mocha.parallelMode(true), 'to satisfy', {
options: {parallel: true},
_runnerClass: stubs.BufferedRunner,
_lazyLoadFiles: true
});
});
});
describe('when parallel mode is already disabled', function() {
beforeEach(function() {
mocha.options.parallel = false;
mocha._runnerClass = Mocha.Runner;
mocha._lazyLoadFiles = false;
});
it('should not swap the Runner, nor change lazy loading setting', function() {
expect(mocha.parallelMode(false), 'to satisfy', {
options: {parallel: false},
_runnerClass: Mocha.Runner,
_lazyLoadFiles: false
});
});
});
describe('when `Mocha` instance in serial mode', function() {
beforeEach(function() {
mocha.options.parallel = false;
});
describe('when passed `true` value', function() {
describe('when `Mocha` instance is in `INIT` state', function() {
beforeEach(function() {
mocha._state = 'init';
// this is broken
this.skip();
});
it('should enable parallel mode', function() {
expect(mocha.parallelMode(true), 'to satisfy', {
_runnerClass: stubs.BufferedRunner,
options: {
parallel: true
},
_lazyLoadFiles: true
});
});
});
describe('when `Mocha` instance is not in `INIT` state', function() {
beforeEach(function() {
mocha._state = 'disposed';
});
it('should throw', function() {
expect(
function() {
mocha.parallelMode(true);
},
'to throw',
{
code: 'ERR_MOCHA_UNSUPPORTED'
}
);
});
});
});
describe('when passed non-`true` value', function() {
describe('when `Mocha` instance is in `INIT` state', function() {
beforeEach(function() {
mocha._state = 'init';
});
it('should enable serial mode', function() {
expect(mocha.parallelMode(0), 'to satisfy', {
_runnerClass: Mocha.Runner,
options: {
parallel: false
},
_lazyLoadFiles: false
});
});
});
});
});
});
});
describe('addFile()', function() {
it('should add the given file to the files array', function() {
mocha.addFile('some-file.js');
expect(mocha.files, 'to exhaustively satisfy', ['some-file.js']);
});
it('should be chainable', function() {
expect(mocha.addFile('some-file.js'), 'to be', mocha);
});
});
describe('loadFiles()', function() {
it('should load all files from the files array', function() {
this.timeout(1000);
mocha.files = [DUMB_FIXTURE_PATH, DUMBER_FIXTURE_PATH];
mocha.loadFiles();
expect(require.cache, 'to have keys', [
DUMB_FIXTURE_PATH,
DUMBER_FIXTURE_PATH
]);
});
it('should execute the optional callback if given', function() {
expect(cb => {
mocha.loadFiles(cb);
}, 'to call the callback');
});
});
describe('unloadFiles()', function() {
it('should delegate Mocha.unloadFile() for each item in its list of files', function() {
mocha.files = [DUMB_FIXTURE_PATH, DUMBER_FIXTURE_PATH];
sandbox.stub(Mocha, 'unloadFile');
mocha.unloadFiles();
expect(Mocha.unloadFile, 'to have a call exhaustively satisfying', [
DUMB_FIXTURE_PATH
])
.and('to have a call exhaustively satisfying', [DUMBER_FIXTURE_PATH])
.and('was called twice');
});
it('should be chainable', function() {
expect(mocha.unloadFiles(), 'to be', mocha);
});
});
describe('reporter()', function() {
describe('when a reporter exists relative to the cwd', function() {
beforeEach(function() {
stubs.utils.cwd.returns(
path.resolve(__dirname, '..', '..', 'lib', 'reporters')
);
});
it('should load from current working directory', function() {
expect(function() {
mocha.reporter('./spec.js');
}, 'not to throw');
});
describe('when the reporter throws upon load', function() {
it('should throw "invalid reporter" exception', function() {
expect(
function() {
mocha.reporter(
'../../test/node-unit/fixtures/wonky-reporter.fixture.js'
);
},
'to throw',
{
code: 'ERR_MOCHA_INVALID_REPORTER'
}
);
});
it('should warn about the error before throwing', function() {
try {
mocha.reporter(
'../../test/node-unit/fixtures/wonky-reporter.fixture.js'
);
} catch (ignored) {
} finally {
expect(stubs.utils.warn, 'to have a call satisfying', [
expect.it('to match', /reporter blew up/)
]);
}
});
});
});
describe('when a reporter exists relative to the "mocha" module path', function() {
it('should load from module path', function() {
expect(function() {
mocha.reporter('./reporters/spec');
}, 'not to throw');
});
describe('when the reporter throws upon load', function() {
it('should throw "invalid reporter" exception', function() {
expect(
function() {
mocha.reporter(
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
);
},
'to throw',
{
code: 'ERR_MOCHA_INVALID_REPORTER'
}
);
});
it('should warn about the error before throwing', function() {
try {
mocha.reporter(
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
);
} catch (ignored) {
} finally {
expect(stubs.utils.warn, 'to have a call satisfying', [
expect.it('to match', /reporter blew up/)
]);
}
});
});
});
});
describe('unloadFiles()', function() {
it('should reset referencesCleaned and allow for next run', function(done) {
mocha.run(function() {
mocha.unloadFiles();
mocha.run(done);
});
});
it('should not be allowed when the current instance is already disposed', function() {
mocha.dispose();
expect(
function() {
mocha.unloadFiles();
},
'to throw',
'Mocha instance is already disposed, it cannot be used again.'
);
});
});
describe('lazyLoadFiles()', function() {
it('should return the `Mocha` instance', function() {
expect(mocha.lazyLoadFiles(), 'to be', mocha);
});
describe('when passed a non-`true` value', function() {
it('should enable eager loading', function() {
mocha.lazyLoadFiles(0);
expect(mocha._lazyLoadFiles, 'to be false');
});
});
describe('when passed `true`', function() {
it('should enable lazy loading', function() {
mocha.lazyLoadFiles(true);
expect(mocha._lazyLoadFiles, 'to be true');
});
});
});
});
describe('static method', function() {
describe('unloadFile()', function() {
it('should unload a specific file from cache', function() {
require(DUMB_FIXTURE_PATH);
Mocha.unloadFile(DUMB_FIXTURE_PATH);
expect(require.cache, 'not to have key', DUMB_FIXTURE_PATH);
});
});
});
});