/
MochaTestRunner.spec.ts
287 lines (248 loc) · 10.8 KB
/
MochaTestRunner.spec.ts
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
import { EventEmitter } from 'events';
import * as path from 'path';
import { commonTokens } from '@stryker-mutator/api/plugin';
import { RunOptions } from '@stryker-mutator/api/test_runner';
import { testInjector } from '@stryker-mutator/test-helpers';
import { expect } from 'chai';
import * as Mocha from 'mocha';
import sinon = require('sinon');
import LibWrapper from '../../src/LibWrapper';
import { MochaOptions } from '../../src-generated/mocha-runner-options';
import { MochaTestRunner } from '../../src/MochaTestRunner';
import { StrykerMochaReporter } from '../../src/StrykerMochaReporter';
import * as utils from '../../src/utils';
describe(MochaTestRunner.name, () => {
let MochaStub: sinon.SinonStub;
let mocha: sinon.SinonStubbedInstance<Mocha> & { suite: sinon.SinonStubbedInstance<EventEmitter> };
let sut: MochaTestRunner;
let requireStub: sinon.SinonStub;
let handleFilesStub: sinon.SinonStub;
let handleRequiresStub: sinon.SinonStub;
let loadRootHooks: sinon.SinonStub;
beforeEach(() => {
MochaStub = sinon.stub(LibWrapper, 'Mocha');
requireStub = sinon.stub(LibWrapper, 'require');
handleFilesStub = sinon.stub(LibWrapper, 'handleFiles');
handleRequiresStub = sinon.stub(LibWrapper, 'handleRequires');
loadRootHooks = sinon.stub(LibWrapper, 'loadRootHooks');
sinon.stub(utils, 'evalGlobal');
mocha = sinon.createStubInstance(Mocha) as any;
mocha.suite = sinon.createStubInstance(EventEmitter) as Mocha.Suite & sinon.SinonStubbedInstance<EventEmitter>;
MochaStub.returns(mocha);
});
afterEach(() => {
// These keys can be used to test the nodejs cache
delete require.cache['foo.js'];
delete require.cache['bar.js'];
delete require.cache['baz.js'];
delete StrykerMochaReporter.log;
});
function createSut(mochaSettings: Partial<{ fileNames: readonly string[]; mochaOptions: Partial<MochaOptions> }>) {
testInjector.options.mochaOptions = mochaSettings.mochaOptions || {};
return testInjector.injector
.provideValue(commonTokens.sandboxFileNames, mochaSettings.fileNames || ['src/math.js', 'test/mathSpec.js'])
.injectClass(MochaTestRunner);
}
it('should set the static `log` property on StrykerMochaReporter', () => {
createSut({});
expect(StrykerMochaReporter.log).eq(testInjector.logger);
});
describe('when mocha version < 6', () => {
let multimatchStub: sinon.SinonStub;
beforeEach(() => {
handleFilesStub.value(undefined);
multimatchStub = sinon.stub(LibWrapper, 'multimatch');
});
it('should log about mocha < 6 detection', async () => {
sut = createSut({});
multimatchStub.returns(['foo.js']);
await sut.init();
expect(testInjector.logger.debug).calledWith('Mocha < 6 detected. Using custom logic to discover files');
});
it('should add discovered test files on run() ', async () => {
multimatchStub.returns(['foo.js', 'bar.js', 'foo2.js']);
sut = createSut({});
await sut.init();
await actRun();
expect(mocha.addFile).calledThrice;
expect(mocha.addFile).calledWith('foo.js');
expect(mocha.addFile).calledWith('foo2.js');
expect(mocha.addFile).calledWith('bar.js');
});
it('should support both `files` as `spec`', async () => {
multimatchStub.returns(['foo.js']);
sut = createSut({
fileNames: ['foo'],
mochaOptions: {
files: ['bar'],
spec: ['foo'],
},
});
await sut.init();
await actRun();
expect(multimatchStub).calledWith(['foo'], [path.resolve('foo'), path.resolve('bar')]);
});
it('should match given file names with configured mocha files as `array`', () => {
const relativeGlobPatterns = ['*.js', 'baz.js'];
const expectedGlobPatterns = relativeGlobPatterns.map((glob) => path.resolve(glob));
actAssertMatchedPatterns(relativeGlobPatterns, expectedGlobPatterns);
});
it('should match given file names with configured mocha files as `string`', () => {
const relativeGlobPattern = '*.js';
const expectedGlobPatterns = [path.resolve(relativeGlobPattern)];
actAssertMatchedPatterns(relativeGlobPattern, expectedGlobPatterns);
});
it('should match given file names with default mocha pattern "test/**/*.js"', () => {
const expectedGlobPatterns = [path.resolve('test/**/*.js')];
actAssertMatchedPatterns(undefined, expectedGlobPatterns);
});
it('should reject if no files could be discovered', async () => {
// Arrange
multimatchStub.returns([]);
const files = ['foo.js', 'bar.js'];
const relativeGlobbing = JSON.stringify(['test/**/*.js'], null, 2);
const absoluteGlobbing = JSON.stringify([path.resolve('test/**/*.js')], null, 2);
const filesStringified = JSON.stringify(files, null, 2);
// Act
sut = createSut({ fileNames: files });
const onGoingWork = sut.init();
// Assert
await expect(onGoingWork).rejectedWith(
`[MochaTestRunner] No files discovered (tried pattern(s) ${relativeGlobbing}). Please specify the files (glob patterns) containing your tests in mochaOptions.spec in your config file.`
);
expect(testInjector.logger.debug).calledWith(`Tried ${absoluteGlobbing} on files: ${filesStringified}.`);
});
function actAssertMatchedPatterns(relativeGlobPatterns: string | string[] | undefined, expectedGlobPatterns: string[]) {
const expectedFiles = ['foo.js', 'bar.js'];
multimatchStub.returns(['foo.js']);
sut = createSut({ fileNames: expectedFiles, mochaOptions: { files: relativeGlobPatterns } });
sut.init();
expect(multimatchStub).calledWith(expectedFiles, expectedGlobPatterns);
}
});
describe('when mocha version >= 6', () => {
let discoveredFiles: string[];
beforeEach(() => {
discoveredFiles = [];
handleFilesStub.returns(discoveredFiles);
});
it('should log about mocha >= 6 detection', async () => {
sut = createSut({});
await sut.init();
expect(testInjector.logger.debug).calledWith("Mocha >= 6 detected. Using mocha's `handleFiles` to load files");
});
it('should mock away the `process.exit` method when calling the mocha function (unfortunate side effect)', async () => {
sut = createSut({});
const originalProcessExit = process.exit;
let stubbedProcessExit = process.exit;
handleFilesStub.callsFake(() => (stubbedProcessExit = process.exit));
await sut.init();
expect(originalProcessExit, "Process.exit doesn't seem to be stubbed away").not.eq(stubbedProcessExit);
expect(originalProcessExit, "Process.exit doesn't seem to be reset").eq(process.exit);
});
it('should pass along supported options to mocha', async () => {
// Arrange
discoveredFiles.push('foo.js', 'bar.js', 'foo2.js');
const mochaOptions: Partial<MochaOptions> = {
['async-only']: true,
grep: 'grepme',
opts: 'opts',
require: [],
timeout: 2000,
ui: 'assert',
};
sut = createSut({ mochaOptions });
await sut.init();
// Act
await actRun();
// Assert
expect(mocha.asyncOnly).called;
expect(mocha.timeout).calledWith(2000);
expect(mocha.ui).calledWith('assert');
expect(mocha.grep).calledWith('grepme');
});
it("should don't set asyncOnly if asyncOnly is false", async () => {
// Arrange
const mochaOptions: Partial<MochaOptions> = {
['async-only']: false,
};
sut = createSut({ mochaOptions });
await sut.init();
// Act
await actRun();
// Assert
expect(mocha.asyncOnly).not.called;
});
it('should pass require additional require options when constructed', async () => {
handleRequiresStub.value(undefined);
const mochaOptions: Partial<MochaOptions> = { require: ['ts-node', 'babel-register'] };
sut = createSut({ mochaOptions });
await sut.init();
expect(requireStub).calledTwice;
expect(requireStub).calledWith('ts-node');
expect(requireStub).calledWith('babel-register');
});
it('should pass and resolve relative require options when constructed', async () => {
handleRequiresStub.value(undefined);
const mochaOptions: Partial<MochaOptions> = { require: ['./setup.js', 'babel-register'] };
sut = createSut({ mochaOptions });
await sut.init();
const resolvedRequire = path.resolve('./setup.js');
expect(requireStub).calledTwice;
expect(requireStub).calledWith(resolvedRequire);
expect(requireStub).calledWith('babel-register');
});
it('should evaluate additional testHooks if required (in global mocha context)', async () => {
discoveredFiles.push('');
sut = createSut({});
await sut.init();
await actRun({ timeout: 0, testHooks: 'foobar();' });
expect(utils.evalGlobal).calledWith('foobar();');
expect(mocha.suite.emit).calledWith('pre-require', global, '', mocha);
expect(mocha.suite.emit).calledWith('require', undefined, '', mocha);
expect(mocha.suite.emit).calledWith('post-require', global, '', mocha);
});
it('should purge cached sandbox files', async () => {
// Arrange
sut = createSut({ fileNames: ['foo.js', 'bar.js'] });
discoveredFiles.push('foo.js'); // should still purge 'bar.js'
sut.init();
require.cache['foo.js'] = 'foo' as any;
require.cache['bar.js'] = 'bar' as any;
require.cache['baz.js'] = 'baz' as any;
// Act
await actRun();
// Assert
expect(require.cache['foo.js']).undefined;
expect(require.cache['bar.js']).undefined;
expect(require.cache['baz.js']).eq('baz');
});
});
describe('when mocha version >=8', () => {
beforeEach(() => {
handleFilesStub.returns(['src/math.js', 'test/math.spec.js']);
});
it('should handle require and allow for rootHooks', async () => {
handleRequiresStub.resolves(['root-hook1', 'bar-hook']);
const mochaOptions: Partial<MochaOptions> = { require: ['./setup.js', 'babel-register'] };
sut = createSut({ mochaOptions });
await sut.init();
expect(handleRequiresStub).calledWithExactly(['./setup.js', 'babel-register']);
expect(loadRootHooks).calledWithExactly(['root-hook1', 'bar-hook']);
});
it('should pass rootHooks to the mocha instance', async () => {
handleRequiresStub.resolves(['root-hook1', 'bar-hook']);
const rootHooks = { beforeEach() {} };
loadRootHooks.resolves(rootHooks);
const mochaOptions: Partial<MochaOptions> = { require: ['./setup.js', 'babel-register'] };
sut = createSut({ mochaOptions });
await sut.init();
await actRun();
expect(LibWrapper.Mocha).calledWithMatch({ rootHooks });
});
});
async function actRun(options: RunOptions = { timeout: 0 }) {
mocha.run.callsArg(0);
return sut.run(options);
}
});