Skip to content

Commit 609f385

Browse files
stipsanboujeepossum
authored andcommitted
Fix --coverage with --findRelatedTests overwriting collectCoverageFrom options (#6736)
* add test to reproduce #6462 * fix test * initial attempt, not happy with it * take 2, happier with this fix * better test case * verify stderr is empty * Update CHANGELOG.md
1 parent b6d5752 commit 609f385

5 files changed

Lines changed: 89 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `[jest-jasmine2]` Use prettier through `require` instead of `localRequire`. Fixes `matchInlineSnapshot` where prettier dependencies like `path` and `fs` are mocked with `jest.mock`. ([#6776](https://github.com/facebook/jest/pull/6776))
1616
- `[docs]` Fix contributors link ([#6711](https://github.com/facebook/jest/pull/6711))
1717
- `[website]` Fix website versions page to link to correct language ([#6734](https://github.com/facebook/jest/pull/6734))
18+
- `[jest-config]` Fix `--coverage` with `--findRelatedTests` overwriting `collectCoverageFrom` options ([#6736](https://github.com/facebook/jest/pull/6736))
1819

1920
## 23.4.1
2021

e2e/__tests__/__snapshots__/find_related_files.test.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`--findRelatedTests flag coverage configuration is applied correctly 1`] = `
4+
"Test Suites: 1 passed, 1 total
5+
Tests: 1 passed, 1 total
6+
Snapshots: 0 total
7+
Time: <<REPLACED>>
8+
Ran all test suites related to files matching /a.js|b.js/i."
9+
`;
10+
11+
exports[`--findRelatedTests flag coverage configuration is applied correctly 2`] = `
12+
"
13+
14+
PASS __tests__/a.test.js
15+
✓ a"
16+
`;
17+
18+
exports[`--findRelatedTests flag coverage configuration is applied correctly 3`] = `
19+
"----------|----------|----------|----------|----------|-------------------|
20+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
21+
----------|----------|----------|----------|----------|-------------------|
22+
All files | 100 | 100 | 100 | 100 | |
23+
a.js | 100 | 100 | 100 | 100 | |
24+
----------|----------|----------|----------|----------|-------------------|"
25+
`;
26+
327
exports[`--findRelatedTests flag generates coverage report for filename 1`] = `
428
"Test Suites: 2 passed, 2 total
529
Tests: 2 passed, 2 total

e2e/__tests__/find_related_files.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,48 @@ describe('--findRelatedTests flag', () => {
9090
// coverage should be collected only for a.js
9191
expect(stdout).toMatchSnapshot();
9292
});
93+
94+
test('coverage configuration is applied correctly', () => {
95+
writeFiles(DIR, {
96+
'.watchmanconfig': '',
97+
'__tests__/a.test.js': `
98+
require('../a');
99+
test('a', () => expect(1).toBe(1));
100+
`,
101+
'a.js': 'module.exports = {}',
102+
'b.js': 'module.exports = {}',
103+
'package.json': JSON.stringify({
104+
jest: {
105+
collectCoverage: true,
106+
collectCoverageFrom: ['!b.js', 'a.js'],
107+
testEnvironment: 'node',
108+
},
109+
}),
110+
});
111+
112+
let stdout;
113+
let stderr;
114+
({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js', 'b.js']));
115+
116+
const {summary, rest} = extractSummary(stderr);
117+
expect(summary).toMatchSnapshot();
118+
expect(
119+
rest
120+
.split('\n')
121+
.map(s => s.trim())
122+
.sort()
123+
.join('\n'),
124+
).toMatchSnapshot();
125+
126+
// Only a.js should be in the report
127+
expect(stdout).toMatchSnapshot();
128+
expect(stdout).toMatch('a.js');
129+
expect(stdout).not.toMatch('b.js');
130+
131+
({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'b.js']));
132+
133+
// Neither a.js or b.js should be in the report
134+
expect(stdout).toMatch('No tests found');
135+
expect(stderr).toBe('');
136+
});
93137
});

packages/jest-config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"jest-resolve": "^23.4.1",
2121
"jest-util": "^23.4.0",
2222
"jest-validate": "^23.4.0",
23+
"micromatch": "^2.3.11",
2324
"pretty-format": "^23.2.0"
2425
}
2526
}

packages/jest-config/src/normalize.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import validatePattern from './validate_pattern';
1818
import {clearLine} from 'jest-util';
1919
import chalk from 'chalk';
2020
import getMaxWorkers from './get_max_workers';
21+
import micromatch from 'micromatch';
2122
import Resolver from 'jest-resolve';
2223
import {replacePathSepForRegex} from 'jest-regex-util';
2324
import {
@@ -677,12 +678,29 @@ export default function normalize(options: InitialOptions, argv: Argv) {
677678
// where arguments to `--collectCoverageFrom` should be globs (or relative
678679
// paths to the rootDir)
679680
if (newOptions.collectCoverage && argv.findRelatedTests) {
680-
newOptions.collectCoverageFrom = argv._.map(filename => {
681+
let collectCoverageFrom = argv._.map(filename => {
681682
filename = replaceRootDirInPath(options.rootDir, filename);
682683
return path.isAbsolute(filename)
683684
? path.relative(options.rootDir, filename)
684685
: filename;
685686
});
687+
688+
// Don't override existing collectCoverageFrom options
689+
if (newOptions.collectCoverageFrom) {
690+
collectCoverageFrom = collectCoverageFrom.reduce((patterns, filename) => {
691+
if (
692+
!micromatch(
693+
[path.relative(options.rootDir, filename)],
694+
newOptions.collectCoverageFrom,
695+
).length
696+
) {
697+
return patterns;
698+
}
699+
return [...patterns, filename];
700+
}, newOptions.collectCoverageFrom);
701+
}
702+
703+
newOptions.collectCoverageFrom = collectCoverageFrom;
686704
}
687705

688706
return {

0 commit comments

Comments
 (0)