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

Exclude setup files from coverage #7790

Merged
merged 10 commits into from
Feb 4, 2019
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
39 changes: 36 additions & 3 deletions packages/jest-runtime/src/__tests__/should_instrument.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
lekterable marked this conversation as resolved.
Show resolved Hide resolved
*/

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 +203,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'],
});
});
});
});
22 changes: 22 additions & 0 deletions packages/jest-runtime/src/shouldInstrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ export default function shouldInstrument(
return false;
}

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

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

if (
config.setupFiles &&
config.setupFiles.some(setupFile => setupFile === filename)
) {
return false;
}

if (
config.setupFilesAfterEnv &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setupFilesAfterEnv and setupFiles are normalized to return empty array, so this extra check is not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thymikee I think they are not normalized for unit tests, just checked and got TypeError: Cannot read property 'some' of undefined, do you want me to leave the check here or provide a default value somewhere?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's because shouldInstrument test file is not typed. Wanna try to add @flow pragma to it and make it use normalized config as it actually expects? If that's too tedious, just pass default values for these specific tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sure, I'll try to take care of this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thymikee I've added the pragma and types, how would I make it use the normalized config tho?

Copy link
Contributor Author

@lekterable lekterable Feb 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that currently, test cases overwrite the default config when they pass their own config object to testShouldInstrument so it still throws a Cannot read property 'some' of undefined error. How about making the testShouldInstrument in should_instrument.test.js merge the default config and the one from args instead of overwriting it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldInstrument expects every config to be normalized. As I said, it's probably to much of a hassle and not in scope of this PR, so I'm good with leaving this as is (although it seems like an area for some small speedup, so definitely worth following up later)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I will try to take care of this in a new PR, thanks for the help!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add TODO comments so we don't forget?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimenB comment added

config.setupFilesAfterEnv.some(setupFile => setupFile === filename)
) {
return false;
}

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