Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --coverage with --findRelatedTests overwriting collectCoverageFrom options #6736

Merged
merged 7 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions e2e/__tests__/__snapshots__/find_related_files.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`--findRelatedTests flag coverage configuration is applied correctly 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites related to files matching /a.js|b.js/i."
`;

exports[`--findRelatedTests flag coverage configuration is applied correctly 2`] = `
"

PASS __tests__/a.test.js
✓ a"
`;

exports[`--findRelatedTests flag coverage configuration is applied correctly 3`] = `
"----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
a.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|"
`;

exports[`--findRelatedTests flag generates coverage report for filename 1`] = `
"Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Expand Down
44 changes: 44 additions & 0 deletions e2e/__tests__/find_related_files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,48 @@ describe('--findRelatedTests flag', () => {
// coverage should be collected only for a.js
expect(stdout).toMatchSnapshot();
});

test('coverage configuration is applied correctly', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/a.test.js': `
require('../a');
test('a', () => expect(1).toBe(1));
`,
'a.js': 'module.exports = {}',
'b.js': 'module.exports = {}',
'package.json': JSON.stringify({
jest: {
collectCoverage: true,
collectCoverageFrom: ['!b.js', 'a.js'],
testEnvironment: 'node',
},
}),
});

let stdout;
let stderr;
({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js', 'b.js']));

const {summary, rest} = extractSummary(stderr);
expect(summary).toMatchSnapshot();
expect(
rest
.split('\n')
.map(s => s.trim())
.sort()
.join('\n'),
).toMatchSnapshot();

// Only a.js should be in the report
expect(stdout).toMatchSnapshot();
expect(stdout).toMatch('a.js');
expect(stdout).not.toMatch('b.js');

({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'b.js']));

// Neither a.js or b.js should be in the report
expect(stdout).toMatch('No tests found');
expect(stderr).toBe('');
});
});
1 change: 1 addition & 0 deletions packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jest-resolve": "^23.4.1",
"jest-util": "^23.4.0",
"jest-validate": "^23.4.0",
"micromatch": "^2.3.11",
"pretty-format": "^23.2.0"
}
}
20 changes: 19 additions & 1 deletion packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import validatePattern from './validate_pattern';
import {clearLine} from 'jest-util';
import chalk from 'chalk';
import getMaxWorkers from './get_max_workers';
import micromatch from 'micromatch';
import Resolver from 'jest-resolve';
import {replacePathSepForRegex} from 'jest-regex-util';
import {
Expand Down Expand Up @@ -677,12 +678,29 @@ export default function normalize(options: InitialOptions, argv: Argv) {
// where arguments to `--collectCoverageFrom` should be globs (or relative
// paths to the rootDir)
if (newOptions.collectCoverage && argv.findRelatedTests) {
newOptions.collectCoverageFrom = argv._.map(filename => {
let collectCoverageFrom = argv._.map(filename => {
filename = replaceRootDirInPath(options.rootDir, filename);
return path.isAbsolute(filename)
? path.relative(options.rootDir, filename)
: filename;
});

// Don't override existing collectCoverageFrom options
if (newOptions.collectCoverageFrom) {
collectCoverageFrom = collectCoverageFrom.reduce((patterns, filename) => {
if (
!micromatch(
[path.relative(options.rootDir, filename)],
newOptions.collectCoverageFrom,
).length
) {
return patterns;
}
return [...patterns, filename];
}, newOptions.collectCoverageFrom);
}

newOptions.collectCoverageFrom = collectCoverageFrom;
}

return {
Expand Down