/
runJestWithCoverage.test.js
117 lines (102 loc) · 2.66 KB
/
runJestWithCoverage.test.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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import runJest from '../runJest';
jest.mock('jest-util', () => {
const util = jest.requireActual('jest-util');
return {
...jest.genMockFromModule('jest-util'),
replacePathSepForGlob: util.replacePathSepForGlob,
};
});
jest.mock(
'../TestScheduler',
() =>
class {
constructor(globalConfig) {
this._globalConfig = globalConfig;
}
scheduleTests() {
return {_globalConfig: this._globalConfig};
}
},
);
jest.mock(
'../TestSequencer',
() =>
class {
sort(allTests) {
return allTests;
}
cacheResults() {}
},
);
jest.mock(
'../SearchSource',
() =>
class {
constructor(context) {
this._context = context;
}
async getTestPaths(globalConfig, changedFilesPromise) {
const {files} = await changedFilesPromise;
const paths = files.filter(path => path.match(/__tests__/));
return {
collectCoverageFrom: files.filter(path => !path.match(/__tests__/)),
tests: paths.map(path => ({
context: this._context,
duration: null,
path,
})),
};
}
},
);
const config = {roots: [], testPathIgnorePatterns: [], testRegex: []};
let globalConfig;
const defaults = {
changedFilesPromise: Promise.resolve({
files: ['foo.js', '__tests__/foo-test.js', 'dont/cover.js'],
}),
contexts: [{config}],
onComplete: runResults => (globalConfig = runResults._globalConfig),
outputStream: {},
startRun: {},
testWatcher: {isInterrupted: () => false},
};
describe('collectCoverageFrom patterns', () => {
it('should apply collectCoverageFrom patterns coming from SearchSource', async () => {
expect.assertions(1);
await runJest({
...defaults,
globalConfig: {
rootDir: '',
},
});
expect(globalConfig.collectCoverageFrom).toEqual([
'foo.js',
'dont/cover.js',
]);
});
it('excludes coverage from files outside the global collectCoverageFrom config', async () => {
expect.assertions(1);
await runJest({
...defaults,
globalConfig: {
collectCoverageFrom: ['**/dont/*.js'],
rootDir: '',
},
});
expect(globalConfig.collectCoverageFrom).toEqual(['dont/cover.js']);
});
it('respects coveragePathIgnorePatterns', async () => {
expect.assertions(1);
await runJest({
...defaults,
globalConfig: {
collectCoverageFrom: ['**/*.js'],
coveragePathIgnorePatterns: ['dont'],
rootDir: '',
},
});
expect(globalConfig.collectCoverageFrom).toEqual(['foo.js']);
});
});