/
options-validator.spec.ts
306 lines (265 loc) · 13.2 KB
/
options-validator.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
297
298
299
300
301
302
303
304
305
306
import os = require('os');
import sinon = require('sinon');
import { strykerCoreSchema, StrykerOptions } from '@stryker-mutator/api/core';
import { testInjector, factory } from '@stryker-mutator/test-helpers';
import { expect } from 'chai';
import { OptionsValidator, validateOptions, markUnknownOptions } from '../../../src/config/options-validator';
import { coreTokens } from '../../../src/di';
describe(OptionsValidator.name, () => {
let sut: OptionsValidator;
beforeEach(() => {
sut = testInjector.injector.provideValue(coreTokens.validationSchema, strykerCoreSchema).injectClass(OptionsValidator);
});
it('should validate an empty object', () => {
sut.validate({});
expect(testInjector.logger.fatal).not.called;
expect(testInjector.logger.error).not.called;
expect(testInjector.logger.warn).not.called;
});
it('should validate the default options', () => {
actAssertValid();
});
describe('thresholds', () => {
it('should be invalid with thresholds < 0 or > 100', () => {
testInjector.options.thresholds.high = -1;
testInjector.options.thresholds.low = 101;
actValidationErrors('Config option "thresholds.high" should be >= 0, was -1.', 'Config option "thresholds.low" should be <= 100, was 101.');
});
it('should be invalid with thresholds.high null', () => {
// @ts-expect-error invalid setting
testInjector.options.thresholds.high = null;
actValidationErrors('Config option "thresholds.high" has the wrong type. It should be a number, but was a null.');
});
it('should not allow high < low', () => {
testInjector.options.thresholds.high = 20;
testInjector.options.thresholds.low = 21;
actValidationErrors('Config option "thresholds.high" should be higher than "thresholds.low".');
});
});
it('should be invalid with invalid logLevel', () => {
// @ts-expect-error invalid setting
testInjector.options.logLevel = 'thisTestPasses';
actValidationErrors(
'Config option "logLevel" should be one of the allowed values ("off", "fatal", "error", "warn", "info", "debug", "trace"), but was "thisTestPasses".'
);
});
it('should be invalid with non-numeric timeoutMS', () => {
breakConfig('timeoutMS', 'break');
actValidationErrors('Config option "timeoutMS" has the wrong type. It should be a number, but was a string.');
});
it('should be invalid with non-numeric timeoutFactor', () => {
breakConfig('timeoutFactor', 'break');
actValidationErrors('Config option "timeoutFactor" has the wrong type. It should be a number, but was a string.');
});
it('should be invalid with non-numeric dryRunTimeout', () => {
breakConfig('dryRunTimeoutMinutes', 'break');
actValidationErrors('Config option "dryRunTimeoutMinutes" has the wrong type. It should be a number, but was a string.');
});
it('should be invalid with negative numeric dryRunTimeout', () => {
breakConfig('dryRunTimeoutMinutes', -1);
actValidationErrors('Config option "dryRunTimeoutMinutes" should be >= 0, was -1.');
});
describe('plugins', () => {
it('should be invalid with non-array plugins', () => {
breakConfig('plugins', '@stryker-mutator/typescript');
actValidationErrors('Config option "plugins" has the wrong type. It should be a array, but was a string.');
});
it('should be invalid with non-string array elements', () => {
breakConfig('plugins', ['stryker-jest', 0]);
actValidationErrors('Config option "plugins[1]" has the wrong type. It should be a string, but was a number.');
});
});
describe('appendPlugins', () => {
it('should be invalid with non-array plugins', () => {
breakConfig('appendPlugins', '@stryker-mutator/typescript');
actValidationErrors('Config option "appendPlugins" has the wrong type. It should be a array, but was a string.');
});
it('should be invalid with non-string array elements', () => {
breakConfig('appendPlugins', ['stryker-jest', 0]);
actValidationErrors('Config option "appendPlugins[1]" has the wrong type. It should be a string, but was a number.');
});
});
describe('mutator', () => {
it('should be invalid with non-string mutator', () => {
// @ts-expect-error invalid setting
testInjector.options.mutator = 1;
actValidationErrors('Config option "mutator" has the wrong type. It should be a object, but was a number.');
});
it('should report a deprecation warning for "mutator.name"', () => {
testInjector.options.mutator = {
// @ts-expect-error invalid setting
name: 'javascript',
};
sut.validate(testInjector.options);
expect(testInjector.logger.warn).calledWith(
'DEPRECATED. Use of "mutator.name" is no longer needed. You can remove "mutator.name" from your configuration. Stryker now supports mutating of JavaScript and friend files out of the box.'
);
});
it('should report a deprecation warning for mutator as a string', () => {
// @ts-expect-error invalid setting
testInjector.options.mutator = 'javascript';
sut.validate(testInjector.options);
expect(testInjector.logger.warn).calledWith(
'DEPRECATED. Use of "mutator" as string is no longer needed. You can remove it from your configuration. Stryker now supports mutating of JavaScript and friend files out of the box.'
);
});
});
describe('testFramework', () => {
it('should report a deprecation warning', () => {
testInjector.options.testFramework = '';
sut.validate(testInjector.options);
expect(testInjector.logger.warn).calledWith(
'DEPRECATED. Use of "testFramework" is no longer needed. You can remove it from your configuration. Your test runner plugin now handles its own test framework integration.'
);
});
});
describe('reporters', () => {
it('should be invalid with non-array reporters', () => {
breakConfig('reporters', '@stryker-mutator/typescript');
actValidationErrors('Config option "reporters" has the wrong type. It should be a array, but was a string.');
});
it('should be invalid with non-string array elements', () => {
breakConfig('reporters', ['stryker-jest', 0]);
actValidationErrors('Config option "reporters[1]" has the wrong type. It should be a string, but was a number.');
});
});
describe('dashboard', () => {
it('should be invalid for non-string project', () => {
breakConfig('dashboard', { project: 23 });
actValidationErrors('Config option "dashboard.project" has the wrong type. It should be a string, but was a number.');
});
it('should be invalid for non-string module', () => {
breakConfig('dashboard', { module: 23 });
actValidationErrors('Config option "dashboard.module" has the wrong type. It should be a string, but was a number.');
});
it('should be invalid for non-string version', () => {
breakConfig('dashboard', { version: 23 });
actValidationErrors('Config option "dashboard.version" has the wrong type. It should be a string, but was a number.');
});
it('should be invalid for non-string baseUrl', () => {
breakConfig('dashboard', { baseUrl: 23 });
actValidationErrors('Config option "dashboard.baseUrl" has the wrong type. It should be a string, but was a number.');
});
it('should be invalid for a wrong reportType', () => {
breakConfig('dashboard', { reportType: 'empty' });
actValidationErrors('Config option "dashboard.reportType" should be one of the allowed values ("full", "mutationScore"), but was "empty".');
});
});
describe('maxConcurrentTestRunners', () => {
it('should report a deprecation warning', () => {
testInjector.options.maxConcurrentTestRunners = 8;
sut.validate(testInjector.options);
expect(testInjector.logger.warn).calledWith('DEPRECATED. Use of "maxConcurrentTestRunners" is deprecated. Please use "concurrency" instead.');
});
it('should not configure "concurrency" if "maxConcurrentTestRunners" is >= cpus-1', () => {
testInjector.options.maxConcurrentTestRunners = 2;
sinon.stub(os, 'cpus').returns([0, 1, 2]);
sut.validate(testInjector.options);
expect(testInjector.options.concurrency).undefined;
});
it('should configure "concurrency" if "maxConcurrentTestRunners" is set with a lower value', () => {
testInjector.options.maxConcurrentTestRunners = 1;
sinon.stub(os, 'cpus').returns([0, 1, 2]);
sut.validate(testInjector.options);
expect(testInjector.options.concurrency).eq(1);
});
});
it('should be invalid with non-numeric maxTestRunnerReuse', () => {
breakConfig('maxTestRunnerReuse', 'break');
actValidationErrors('Config option "maxTestRunnerReuse" has the wrong type. It should be a number, but was a string.');
});
it('should warn when testRunnerNodeArgs are combined with the "command" test runner', () => {
testInjector.options.testRunnerNodeArgs = ['--inspect-brk'];
testInjector.options.testRunner = 'command';
sut.validate(testInjector.options);
expect(testInjector.logger.warn).calledWith(
'Using "testRunnerNodeArgs" together with the "command" test runner is not supported, these arguments will be ignored. You can add your custom arguments by setting the "commandRunner.command" option.'
);
});
describe('transpilers', () => {
it('should report a deprecation warning', () => {
testInjector.options.transpilers = ['stryker-jest'];
sut.validate(testInjector.options);
expect(testInjector.logger.warn).calledWith(
'DEPRECATED. Support for "transpilers" is removed. You can now configure your own "buildCommand". For example, npm run build.'
);
});
});
it('should be invalid with invalid coverageAnalysis', () => {
breakConfig('coverageAnalysis', 'invalid');
actValidationErrors('Config option "coverageAnalysis" should be one of the allowed values ("off", "all", "perTest"), but was "invalid".');
});
function actValidationErrors(...expectedErrors: string[]) {
expect(() => sut.validate(testInjector.options)).throws();
for (const error of expectedErrors) {
expect(testInjector.logger.error).calledWith(error);
}
expect(testInjector.logger.error).callCount(expectedErrors.length);
}
function actAssertValid() {
sut.validate(testInjector.options);
expect(testInjector.logger.fatal).not.called;
expect(testInjector.logger.error).not.called;
expect(testInjector.logger.warn).not.called;
}
function breakConfig(key: keyof StrykerOptions, value: any): void {
const original = testInjector.options[key];
if (typeof original === 'object' && !Array.isArray(original)) {
testInjector.options[key] = { ...original, ...value };
} else {
testInjector.options[key] = value;
}
}
});
describe(validateOptions.name, () => {
let optionsValidatorMock: sinon.SinonStubbedInstance<OptionsValidator>;
beforeEach(() => {
optionsValidatorMock = sinon.createStubInstance(OptionsValidator);
});
it('should validate the options using given optionsValidator', () => {
const options = { foo: 'bar' };
const output = validateOptions(options, (optionsValidatorMock as unknown) as OptionsValidator);
expect(options).deep.eq(output);
expect(optionsValidatorMock.validate).calledWith(options);
});
});
describe(markUnknownOptions.name, () => {
it('should not warn when there are no unknown properties', () => {
testInjector.options.htmlReporter = {
baseDir: 'test',
};
expect(testInjector.logger.warn).not.called;
});
it('should return the options, no matter what', () => {
testInjector.options['this key does not exist'] = 'foo';
const output = markUnknownOptions(testInjector.options, strykerCoreSchema, testInjector.logger);
expect(output).eq(testInjector.options);
});
it('should not warn when unknown properties are postfixed with "_comment"', () => {
testInjector.options['maxConcurrentTestRunners_comment'] = 'Recommended to use half of your cores';
markUnknownOptions(testInjector.options, strykerCoreSchema, testInjector.logger);
expect(testInjector.logger.warn).not.called;
});
it('should warn about unknown properties', () => {
testInjector.options['karma'] = {};
testInjector.options['jest'] = {};
markUnknownOptions(testInjector.options, strykerCoreSchema, testInjector.logger);
expect(testInjector.logger.warn).calledThrice;
expect(testInjector.logger.warn).calledWith('Unknown stryker config option "karma".');
expect(testInjector.logger.warn).calledWith('Unknown stryker config option "jest".');
expect(testInjector.logger.warn).calledWithMatch('Possible causes');
});
it('should not warn about unknown properties when warnings are disabled', () => {
testInjector.options['karma'] = {};
testInjector.options.warnings = factory.warningOptions({ unknownOptions: false });
markUnknownOptions(testInjector.options, strykerCoreSchema, testInjector.logger);
expect(testInjector.logger.warn).not.called;
});
it('should ignore options added by Stryker itself', () => {
testInjector.options['set'] = {};
testInjector.options['configFile'] = {};
testInjector.options['$schema'] = '';
markUnknownOptions(testInjector.options, strykerCoreSchema, testInjector.logger);
expect(testInjector.logger.warn).not.called;
});
});