/
MochaOptionsLoader.spec.ts
296 lines (267 loc) · 9.82 KB
/
MochaOptionsLoader.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
288
289
290
291
292
293
294
295
296
import * as fs from 'fs';
import * as path from 'path';
import sinon = require('sinon');
import { expect } from 'chai';
import { testInjector } from '@stryker-mutator/test-helpers';
import LibWrapper from '../../src/LibWrapper';
import { MochaOptions } from '../../src-generated/mocha-runner-options';
import MochaOptionsLoader from '../../src/MochaOptionsLoader';
import { MochaRunnerWithStrykerOptions } from '../../src/MochaRunnerWithStrykerOptions';
describe(MochaOptionsLoader.name, () => {
let readFileStub: sinon.SinonStub;
let existsFileStub: sinon.SinonStub;
let options: MochaRunnerWithStrykerOptions;
let sut: MochaOptionsLoader;
beforeEach(() => {
sut = testInjector.injector.injectClass(MochaOptionsLoader);
options = testInjector.options as MochaRunnerWithStrykerOptions;
options.mochaOptions = {};
});
describe('with mocha >= 6', () => {
let rawOptions: { [option: string]: any };
beforeEach(() => {
rawOptions = Object.create(null);
sinon.stub(LibWrapper, 'loadOptions').returns(rawOptions);
});
it('should log about mocha >= 6', () => {
sut.load(options);
expect(testInjector.logger.debug).calledWith(
"Mocha >= 6 detected. Using mocha's `%s` to load mocha options",
LibWrapper.loadOptions && LibWrapper.loadOptions.name
);
});
it('should call `loadOptions` with serialized arguments', () => {
options.mochaOptions = {
'no-config': true,
'no-opts': true,
'no-package': true,
package: 'alternative-package.json',
config: 'alternative-config.yaml',
opts: 'alternative.opts',
extension: ['.js'], // should not appear in serialized arguments
};
sut.load(options);
expect(LibWrapper.loadOptions).calledWith([
'--no-config',
'--no-opts',
'--no-package',
'--package',
'alternative-package.json',
'--opts',
'alternative.opts',
'--config',
'alternative-config.yaml',
]);
});
it('should filter out invalid options from the `loadOptions` result', () => {
// Following are valid options
rawOptions.extension = ['foo'];
rawOptions.require = ['bar'];
rawOptions.timeout = 4200;
rawOptions['async-only'] = true;
rawOptions.ui = 'quux';
rawOptions.grep = 'quuz';
rawOptions.ignore = ['garply'];
rawOptions.file = ['grault'];
rawOptions.spec = ['test/**/*.js'];
rawOptions.garply = 'waldo'; // this should be filtered out
const result = sut.load(options);
const expected: MochaOptions = createMochaOptions({
extension: ['foo'],
file: ['grault'],
grep: 'quuz',
ignore: ['garply'],
opts: './test/mocha.opts',
require: ['bar'],
spec: ['test/**/*.js'],
timeout: 4200,
'async-only': true,
ui: 'quux',
});
expect(result).deep.eq({ ...expected });
});
it('should trace log the mocha call', () => {
testInjector.logger.isTraceEnabled.returns(true);
options.mochaOptions = {
'no-config': true,
};
rawOptions.baz = 'qux';
sut.load(options);
const fnName = LibWrapper.loadOptions && LibWrapper.loadOptions.name;
expect(testInjector.logger.trace).calledWith(`Mocha: ${fnName}(['--no-config']) => {"baz":"qux"}`);
});
it("should respect mocha's defaults", () => {
const actualOptions = sut.load(options);
expect(actualOptions).deep.eq(createMochaOptions());
});
it('should not allow to set parallel', () => {
rawOptions['parallel'] = true;
const actualOptions = sut.load(options);
expect((actualOptions as any).parallel).undefined;
});
});
describe('with mocha < 6', () => {
beforeEach(() => {
sinon.stub(LibWrapper, 'loadOptions').value(undefined);
readFileStub = sinon.stub(fs, 'readFileSync');
existsFileStub = sinon.stub(fs, 'existsSync').returns(true);
});
it('should log about mocha < 6', () => {
existsFileStub.returns(false);
sut.load(options);
expect(testInjector.logger.debug).calledWith('Mocha < 6 detected. Using custom logic to parse mocha options');
});
it('should log deprecated mocha version warning', async () => {
existsFileStub.returns(false);
sut.load(options);
expect(testInjector.logger.warn).calledWith('DEPRECATED: Mocha < 6 detected. Please upgrade to at least Mocha version 6.');
});
it('should load a mocha.opts file if specified', () => {
readFileStub.returns('');
options.mochaOptions = {
opts: 'some/mocha.opts/file',
};
sut.load(options);
expect(testInjector.logger.info).calledWith(`Loading mochaOpts from "${path.resolve('some/mocha.opts/file')}"`);
expect(fs.readFileSync).calledWith(path.resolve('some/mocha.opts/file'));
});
it("should log an error if specified mocha.opts file doesn't exist", () => {
readFileStub.returns('');
existsFileStub.returns(false);
options.mochaOptions = {
opts: 'some/mocha.opts/file',
};
sut.load(options);
expect(testInjector.logger.error).calledWith(
`Could not load opts from "${path.resolve('some/mocha.opts/file')}". Please make sure opts file exists.`
);
});
it('should load default mocha.opts file if not specified', () => {
readFileStub.returns('');
sut.load(options);
expect(testInjector.logger.info).calledWith(`Loading mochaOpts from "${path.resolve('test/mocha.opts')}"`);
expect(fs.readFileSync).calledWith(path.resolve('./test/mocha.opts'));
});
it("shouldn't load anything if mocha['no-opts'] = true", () => {
options.mochaOptions = {
'no-opts': true,
};
sut.load(options);
expect(fs.readFileSync).not.called;
expect(testInjector.logger.debug).calledWith('Not reading additional mochaOpts from a file');
});
it('should not load default mocha.opts file if not found', () => {
existsFileStub.returns(false);
const mochaOptions = sut.load(options);
expect(mochaOptions).deep.eq(createMochaOptions());
expect(testInjector.logger.debug).calledWith(
'No mocha opts file found, not loading additional mocha options (%s was not defined).',
'mochaOptions.opts'
);
});
it('should load `--require` and `-r` properties if specified in mocha.opts file', () => {
readFileStub.returns(`
--require src/test/support/setup
-r babel-require
`);
options.mochaOptions = { opts: '.' };
const mochaOptions = sut.load(options);
expect(mochaOptions).deep.include({
require: ['src/test/support/setup', 'babel-require'],
});
});
function itShouldLoadProperty(property: string, value: string, expectedConfig: Partial<MochaOptions>) {
it(`should load '${property} if specified`, () => {
readFileStub.returns(`${property} ${value}`);
options.mochaOptions = { opts: 'path/to/opts/file' };
expect(sut.load(options)).deep.include(expectedConfig);
});
}
itShouldLoadProperty('--timeout', '2000', { timeout: 2000 });
itShouldLoadProperty('-t', '2000', { timeout: 2000 });
itShouldLoadProperty('-A', '', { 'async-only': true });
itShouldLoadProperty('--async-only', '', { 'async-only': true });
itShouldLoadProperty('--ui', 'qunit', { ui: 'qunit' });
itShouldLoadProperty('-u', 'qunit', { ui: 'qunit' });
itShouldLoadProperty('-g', 'grepthis', { grep: 'grepthis' });
itShouldLoadProperty('--grep', '/grep(this|that)/', { grep: 'grep(this|that)' });
itShouldLoadProperty('--grep', 'grep(this|that)?', { grep: 'grep(this|that)?' });
it('should not override additional properties (except for defaults)', () => {
readFileStub.returns(`
-u qunit
-t 2000
-A
-r babel-register
`);
options.mochaOptions = {
'async-only': false,
opts: 'path/to/opts/file',
require: ['ts-node/register'],
timeout: 4000,
ui: 'exports',
};
const mochaOptions = sut.load(options);
expect(mochaOptions).deep.equal(
createMochaOptions({
'async-only': false,
extension: ['js'],
opts: 'path/to/opts/file',
require: ['ts-node/register'],
spec: ['test'],
timeout: 4000,
ui: 'exports',
})
);
});
it('should ignore additional properties', () => {
readFileStub.returns(`
--reporter dot
--ignore-leaks
`);
options.mochaOptions = {
opts: 'some/mocha.opts/file',
};
const mochaOptions = sut.load(options);
expect(mochaOptions).not.have.property('reporter');
expect(mochaOptions).not.have.property('ignore-leaks');
expect(testInjector.logger.debug).calledWith('Ignoring option "--reporter" as it is not supported.');
expect(testInjector.logger.debug).calledWith('Ignoring option "--ignore-leaks" as it is not supported.');
});
it('should ignore invalid --ui and --timeout options', () => {
readFileStub.returns(`
--timeout
--ui
`);
options.mochaOptions = {
opts: 'some/mocha.opts/file',
};
const mochaOptions = sut.load(options);
expect(mochaOptions).deep.eq(
createMochaOptions({
extension: ['js'],
opts: 'some/mocha.opts/file',
spec: ['test'],
timeout: 2000,
ui: 'bdd',
})
);
});
});
function createMochaOptions(overrides?: Partial<MochaOptions>): MochaOptions {
return {
'async-only': false,
'no-config': false,
'no-opts': false,
require: [],
'no-package': false,
extension: ['js'],
file: [],
ignore: [],
opts: './test/mocha.opts',
spec: ['test'],
timeout: 2000,
ui: 'bdd',
...overrides,
};
}
});