/
project-references.it.spec.ts
71 lines (61 loc) · 2.46 KB
/
project-references.it.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
import path from 'path';
import fs from 'fs';
import { expect } from 'chai';
import { Mutant, Range } from '@stryker-mutator/api/core';
import { CheckResult, CheckStatus } from '@stryker-mutator/api/check';
import { testInjector, factory } from '@stryker-mutator/test-helpers';
import { createTypescriptChecker } from '../../src';
import { TypescriptChecker } from '../../src/typescript-checker';
const resolveTestResource = (path.resolve.bind(
path,
__dirname,
'..' /* integration */,
'..' /* test */,
'..' /* dist */,
'testResources',
'project-references'
) as unknown) as typeof path.resolve;
describe('Typescript checker on a project with project references', () => {
let sut: TypescriptChecker;
beforeEach(() => {
testInjector.options.tsconfigFile = resolveTestResource('tsconfig.root.json');
sut = testInjector.injector.injectFunction(createTypescriptChecker);
return sut.init();
});
it('should not write output to disk', () => {
expect(fs.existsSync(resolveTestResource('dist')), 'Output was written to disk!').false;
});
it('should be able to validate a mutant', async () => {
const mutant = createMutant('src/todo.ts', 'TodoList.allTodos.push(newItem)', 'newItem ? 42 : 43');
const expectedResult: CheckResult = {
status: CheckStatus.Passed,
};
const actualResult = await sut.check(mutant);
expect(actualResult).deep.eq(expectedResult);
});
it('should allow unused local variables (override options)', async () => {
const mutant = createMutant('src/todo.ts', 'TodoList.allTodos.push(newItem)', '42');
const expectedResult: CheckResult = {
status: CheckStatus.Passed,
};
const actual = await sut.check(mutant);
expect(actual).deep.eq(expectedResult);
});
});
const fileContents = Object.freeze({
['src/todo.ts']: fs.readFileSync(resolveTestResource('src', 'todo.ts'), 'utf8'),
['test/todo.spec.ts']: fs.readFileSync(resolveTestResource('test', 'todo.spec.ts'), 'utf8'),
});
function createMutant(fileName: 'src/todo.ts' | 'test/todo.spec.ts', findText: string, replacement: string, offset = 0): Mutant {
const originalOffset: number = fileContents[fileName].indexOf(findText);
if (originalOffset === -1) {
throw new Error(`Cannot find ${findText} in ${fileName}`);
}
const range: Range = [originalOffset + offset, originalOffset + findText.length];
return factory.mutant({
fileName: resolveTestResource(fileName),
mutatorName: 'foo-mutator',
range,
replacement,
});
}