Skip to content

Commit

Permalink
Exclude setup files from coverage (#7790)
Browse files Browse the repository at this point in the history
  • Loading branch information
lekterable authored and SimenB committed Feb 4, 2019
1 parent 74ae342 commit 012820e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- `[jest-config]` Allow `moduleFileExtensions` without 'js' for custom runners ([#7751](https://github.com/facebook/jest/pull/7751))
- `[jest-cli]` Load transformers before installing require hooks ([#7752](https://github.com/facebook/jest/pull/7752)
- `[jest-cli]` Handle missing `numTodoTests` in test results ([#7779](https://github.com/facebook/jest/pull/7779))
- `[jest-runtime]` Exclude setup/teardown files from coverage report ([#7790](https://github.com/facebook/jest/pull/7790)

### Chore & Maintenance

Expand Down
44 changes: 24 additions & 20 deletions e2e/__tests__/__snapshots__/coverageReport.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ exports[`collects coverage only from multiple specified files 1`] = `
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
file.js | 100 | 100 | 100 | 100 | |
otherFile.js | 100 | 100 | 100 | 100 | |
setup.js | 100 | 100 | 100 | 100 | |
--------------|----------|----------|----------|----------|-------------------|
`;

Expand All @@ -27,16 +27,16 @@ exports[`collects coverage only from specified file 1`] = `
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
setup.js | 100 | 100 | 100 | 100 | |
file.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
`;

exports[`collects coverage only from specified files avoiding dependencies 1`] = `
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 85.71 | 100 | 50 | 100 | |
sum.js | 85.71 | 100 | 50 | 100 | |
All files | 87.5 | 100 | 50 | 100 | |
sum.js | 87.5 | 100 | 50 | 100 | |
----------|----------|----------|----------|----------|-------------------|
`;

Expand All @@ -46,11 +46,12 @@ exports[`generates coverage when using the testRegex config param 1`] = `
-------------------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------------------------------|----------|----------|----------|----------|-------------------|
All files | 56.52 | 0 | 50 | 55.56 | |
coverage-report | 41.18 | 0 | 25 | 42.86 | |
All files | 60 | 0 | 50 | 60 | |
coverage-report | 47.37 | 0 | 25 | 50 | |
file.js | 100 | 100 | 100 | 100 | |
notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8,15,16,17,19 |
otherFile.js | 100 | 100 | 100 | 100 | |
sum.js | 85.71 | 100 | 50 | 100 | |
sum.js | 87.5 | 100 | 50 | 100 | |
sumDependency.js | 0 | 0 | 0 | 0 | 8,10,12 |
coverage-report/cached-duplicates/a | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
Expand All @@ -71,11 +72,12 @@ exports[`outputs coverage report 1`] = `
-------------------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------------------------------|----------|----------|----------|----------|-------------------|
All files | 56.52 | 0 | 50 | 55.56 | |
coverage-report | 41.18 | 0 | 25 | 42.86 | |
All files | 60 | 0 | 50 | 60 | |
coverage-report | 47.37 | 0 | 25 | 50 | |
file.js | 100 | 100 | 100 | 100 | |
notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8,15,16,17,19 |
otherFile.js | 100 | 100 | 100 | 100 | |
sum.js | 85.71 | 100 | 50 | 100 | |
sum.js | 87.5 | 100 | 50 | 100 | |
sumDependency.js | 0 | 0 | 0 | 0 | 8,10,12 |
coverage-report/cached-duplicates/a | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
Expand All @@ -87,19 +89,20 @@ All files | 56.52 | 0 | 50 | 55.56
exports[`outputs coverage report when text and text-summary is requested 1`] = `
=============================== Coverage summary ===============================
Statements : 56.52% ( 13/23 )
Statements : 60% ( 15/25 )
Branches : 0% ( 0/4 )
Functions : 50% ( 3/6 )
Lines : 55.56% ( 10/18 )
Lines : 60% ( 12/20 )
================================================================================
-------------------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------------------------------|----------|----------|----------|----------|-------------------|
All files | 56.52 | 0 | 50 | 55.56 | |
coverage-report | 41.18 | 0 | 25 | 42.86 | |
All files | 60 | 0 | 50 | 60 | |
coverage-report | 47.37 | 0 | 25 | 50 | |
file.js | 100 | 100 | 100 | 100 | |
notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8,15,16,17,19 |
otherFile.js | 100 | 100 | 100 | 100 | |
sum.js | 85.71 | 100 | 50 | 100 | |
sum.js | 87.5 | 100 | 50 | 100 | |
sumDependency.js | 0 | 0 | 0 | 0 | 8,10,12 |
coverage-report/cached-duplicates/a | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
Expand All @@ -112,11 +115,12 @@ exports[`outputs coverage report when text is requested 1`] = `
-------------------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------------------------------|----------|----------|----------|----------|-------------------|
All files | 56.52 | 0 | 50 | 55.56 | |
coverage-report | 41.18 | 0 | 25 | 42.86 | |
All files | 60 | 0 | 50 | 60 | |
coverage-report | 47.37 | 0 | 25 | 50 | |
file.js | 100 | 100 | 100 | 100 | |
notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8,15,16,17,19 |
otherFile.js | 100 | 100 | 100 | 100 | |
sum.js | 85.71 | 100 | 50 | 100 | |
sum.js | 87.5 | 100 | 50 | 100 | |
sumDependency.js | 0 | 0 | 0 | 0 | 8,10,12 |
coverage-report/cached-duplicates/a | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
Expand All @@ -128,9 +132,9 @@ All files | 56.52 | 0 | 50 | 55.56
exports[`outputs coverage report when text-summary is requested 1`] = `
=============================== Coverage summary ===============================
Statements : 56.52% ( 13/23 )
Statements : 60% ( 15/25 )
Branches : 0% ( 0/4 )
Functions : 50% ( 3/6 )
Lines : 55.56% ( 10/18 )
Lines : 60% ( 12/20 )
================================================================================
`;
6 changes: 3 additions & 3 deletions e2e/__tests__/coverageReport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ test('collects coverage only from specified file', () => {
'--no-cache',
'--coverage',
'--collectCoverageFrom', // overwrites the one in package.json
'setup.js',
'file.js',
],
{stripAnsi: true},
);

// Coverage report should only have `setup.js` coverage info
// Coverage report should only have `file.js` coverage info
expect(wrap(stdout)).toMatchSnapshot();
});

Expand All @@ -55,7 +55,7 @@ test('collects coverage only from multiple specified files', () => {
'--no-cache',
'--coverage',
'--collectCoverageFrom',
'setup.js',
'file.js',
'--collectCoverageFrom',
'otherFile.js',
],
Expand Down
8 changes: 8 additions & 0 deletions e2e/coverage-report/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {a: 4};
1 change: 1 addition & 0 deletions e2e/coverage-report/sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require('./sumDependency.js');
require('./otherFile');
require('./file');

const uncoveredFunction = () => 1 + 'abc';

Expand Down
38 changes: 35 additions & 3 deletions packages/jest-runtime/src/__tests__/should_instrument.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
*
*/

import {normalize} from 'jest-config';
import shouldInstrument from '../shouldInstrument';

describe('shouldInstrument', () => {
const defaultFilename = 'source_file.test.js';
const defaultOptions = {
collectCoverage: true,
};
const defaultConfig = {
rootDir: '/',
};
const defaultConfig = normalize(
{
rootDir: '/',
},
{},
).options;

describe('should return true', () => {
const testShouldInstrument = (
Expand Down Expand Up @@ -198,5 +202,33 @@ describe('shouldInstrument', () => {

testShouldInstrument(filename, defaultOptions, defaultConfig);
});

it('if file is a globalSetup file', () => {
testShouldInstrument('globalSetup.js', defaultOptions, {
globalSetup: 'globalSetup.js',
rootDir: '/',
});
});

it('if file is globalTeardown file', () => {
testShouldInstrument('globalTeardown.js', defaultOptions, {
globalTeardown: 'globalTeardown.js',
rootDir: '/',
});
});

it('if file is in setupFiles', () => {
testShouldInstrument('setupTest.js', defaultOptions, {
rootDir: '/',
setupFiles: ['setupTest.js'],
});
});

it('if file is in setupFilesAfterEnv', () => {
testShouldInstrument('setupTest.js', defaultOptions, {
rootDir: '/',
setupFilesAfterEnv: ['setupTest.js'],
});
});
});
});
24 changes: 24 additions & 0 deletions packages/jest-runtime/src/shouldInstrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ export default function shouldInstrument(
return false;
}

if (config.globalSetup === filename) {
return false;
}

if (config.globalTeardown === filename) {
return false;
}

if (
//TODO: Remove additional check when normalized config provided in unit test
config.setupFiles &&
config.setupFiles.some(setupFile => setupFile === filename)
) {
return false;
}

if (
//TODO: Remove additional check when normalized config provided in unit test
config.setupFilesAfterEnv &&
config.setupFilesAfterEnv.some(setupFile => setupFile === filename)
) {
return false;
}

if (MOCKS_PATTERN.test(filename)) {
return false;
}
Expand Down

0 comments on commit 012820e

Please sign in to comment.