Skip to content

Commit 6ea16b5

Browse files
authored
config#collectCoverageFrom (#1349)
1 parent 880e0a8 commit 6ea16b5

File tree

14 files changed

+116
-22
lines changed

14 files changed

+116
-22
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
exports[`test collects coverage only from specified files 1`] = `
2+
"Using <<REPLACED>>, jasmine2
3+
----------|----------|----------|----------|----------|----------------|
4+
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
5+
----------|----------|----------|----------|----------|----------------|
6+
All files | 100 | 100 | 100 | 100 | |
7+
setup.js | 100 | 100 | 100 | 100 | |
8+
----------|----------|----------|----------|----------|----------------|
9+
"
10+
`;
11+
12+
exports[`test outputs coverage report 1`] = `
13+
"Using <<REPLACED>>, jasmine2
14+
---------------|----------|----------|----------|----------|----------------|
15+
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
16+
---------------|----------|----------|----------|----------|----------------|
17+
All files | 100 | 100 | 100 | 100 | |
18+
other-file.js | 100 | 100 | 100 | 100 | |
19+
sum.js | 100 | 100 | 100 | 100 | |
20+
---------------|----------|----------|----------|----------|----------------|
21+
"
22+
`;

integration_tests/__tests__/coverage_report-test.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,29 @@
99
*/
1010
'use strict';
1111

12+
const {stripJestVersion} = require('../utils');
1213
const runJest = require('../runJest');
1314
const fs = require('fs');
1415
const path = require('path');
1516

16-
describe('Coverage Report', () => {
17-
it('outputs coverage report', () => {
18-
const result = runJest('coverage_report', ['--coverage']);
19-
const stdout = result.stdout.toString();
17+
it('outputs coverage report', () => {
18+
const {stdout, status} = runJest('coverage_report', ['--coverage']);
19+
const coverageDir = path.resolve(__dirname, '../coverage_report/coverage');
2020

21-
const coverageDir = path.resolve(__dirname, '../coverage_report/coverage');
22-
expect(stdout).toMatch(/All files.*100.*100.*100.*100/);
23-
expect(stdout).not.toMatch(/^.+__tests__.+\|.+\|.+\|.+\|/gm);
24-
expect(stdout).not.toMatch(/^.+__mocks__.+\|.+\|.+\|.+\|/gm);
21+
// should be no `setup.file` in the coverage report. It's ignored
22+
expect(stripJestVersion(stdout)).toMatchSnapshot();
2523

26-
// Make sure `coveragePathIgnorePatterns` works
27-
expect(stdout).not.toMatch('setup.js');
24+
expect(() => fs.accessSync(coverageDir, fs.F_OK)).not.toThrow();
25+
expect(status).toBe(0);
26+
});
27+
28+
it('collects coverage only from specified files', () => {
29+
const {stdout} = runJest('coverage_report', [
30+
'--coverage',
31+
'--collectCoverageFrom', // overwrites the one in package.json
32+
'setup.js',
33+
]);
2834

29-
// this will throw if the coverage directory is not there
30-
fs.accessSync(coverageDir, fs.F_OK);
31-
expect(result.status).toBe(0);
32-
});
35+
// Coverage report should only have `setup.js` coverage info
36+
expect(stripJestVersion(stdout)).toMatchSnapshot();
3337
});

integration_tests/__tests__/transform-test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010

1111
jest.unmock('../runJest');
1212

13-
const {linkJestPackage, run} = require('../utils');
13+
const {linkJestPackage, run, stripJestVersion} = require('../utils');
1414
const path = require('path');
1515
const runJest = require('../runJest');
1616

17-
const stripJestVersion = stdout =>
18-
stdout.replace(/Jest CLI v\d{1,2}\.\d{1,2}\.\d{1,2}/, '<<REPLACED>>');
19-
2017
describe('babel-jest', () => {
2118
const dir = path.resolve(__dirname, '..', 'transform/babel-jest');
2219

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
'use strict';
9+
10+
module.exports = {a: 5};

integration_tests/coverage_report/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"jest": {
3-
"testEnvironment": "node",
4-
"coveragePathIgnorePatterns": [
5-
"<rootDir>/setup.js",
6-
"packages\/jest"
3+
"collectCoverageFrom": [
4+
"*.js",
5+
"!setup.js"
76
],
87
"setupFiles": [
98
"<rootDir>/setup.js"

integration_tests/coverage_report/sum.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'use strict';
99

1010
require('./sum_dependency.js');
11+
require('./other-file');
1112

1213
module.exports = function(a, b) {
1314
return a + b;

integration_tests/transform/custom-instrumenting-preprocessor/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88

99
'use strict';
1010

11+
require('./some-other-file');
12+
1113
module.exports = {a: 1};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = {a: 1};

integration_tests/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ const fileExists = filePath => {
4646
}
4747
};
4848

49+
const stripJestVersion = stdout =>
50+
stdout.replace(/Jest CLI v\d{1,2}\.\d{1,2}\.\d{1,2}/, '<<REPLACED>>');
51+
4952
module.exports = {
5053
fileExists,
5154
linkJestPackage,
5255
run,
56+
stripJestVersion,
5357
};

packages/jest-cli/src/cli/args.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ const options = {
6060
),
6161
type: 'boolean',
6262
},
63+
collectCoverageFrom: {
64+
description: wrap(
65+
'relative to <rootDir> glob pattern matching the files that coverage ' +
66+
'info needs to be collected from.',
67+
),
68+
type: 'string',
69+
},
6370
maxWorkers: {
6471
alias: 'w',
6572
description: wrap(

0 commit comments

Comments
 (0)