From 012820ee00af31da1767acfec3c29821dfab5e49 Mon Sep 17 00:00:00 2001 From: Kornel Dubieniecki Date: Mon, 4 Feb 2019 10:26:38 +0100 Subject: [PATCH] Exclude setup files from coverage (#7790) --- CHANGELOG.md | 1 + .../__snapshots__/coverageReport.test.js.snap | 44 ++++++++++--------- e2e/__tests__/coverageReport.test.js | 6 +-- e2e/coverage-report/file.js | 8 ++++ e2e/coverage-report/sum.js | 1 + .../src/__tests__/should_instrument.test.js | 38 ++++++++++++++-- packages/jest-runtime/src/shouldInstrument.js | 24 ++++++++++ 7 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 e2e/coverage-report/file.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f0cd29a31e2..86f2904e9ca8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/e2e/__tests__/__snapshots__/coverageReport.test.js.snap b/e2e/__tests__/__snapshots__/coverageReport.test.js.snap index ad619cfc44f1..104b7cb67776 100644 --- a/e2e/__tests__/__snapshots__/coverageReport.test.js.snap +++ b/e2e/__tests__/__snapshots__/coverageReport.test.js.snap @@ -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 | | --------------|----------|----------|----------|----------|-------------------| `; @@ -27,7 +27,7 @@ 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 | | ----------|----------|----------|----------|----------|-------------------| `; @@ -35,8 +35,8 @@ 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 | | ----------|----------|----------|----------|----------|-------------------| `; @@ -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 | | @@ -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 | | @@ -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 | | @@ -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 | | @@ -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 ) ================================================================================ `; diff --git a/e2e/__tests__/coverageReport.test.js b/e2e/__tests__/coverageReport.test.js index b17fd88e85e4..dc50c3016c08 100644 --- a/e2e/__tests__/coverageReport.test.js +++ b/e2e/__tests__/coverageReport.test.js @@ -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(); }); @@ -55,7 +55,7 @@ test('collects coverage only from multiple specified files', () => { '--no-cache', '--coverage', '--collectCoverageFrom', - 'setup.js', + 'file.js', '--collectCoverageFrom', 'otherFile.js', ], diff --git a/e2e/coverage-report/file.js b/e2e/coverage-report/file.js new file mode 100644 index 000000000000..cae7a2495630 --- /dev/null +++ b/e2e/coverage-report/file.js @@ -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}; diff --git a/e2e/coverage-report/sum.js b/e2e/coverage-report/sum.js index 27e339dbed42..13420684e45e 100644 --- a/e2e/coverage-report/sum.js +++ b/e2e/coverage-report/sum.js @@ -7,6 +7,7 @@ require('./sumDependency.js'); require('./otherFile'); +require('./file'); const uncoveredFunction = () => 1 + 'abc'; diff --git a/packages/jest-runtime/src/__tests__/should_instrument.test.js b/packages/jest-runtime/src/__tests__/should_instrument.test.js index 376e8e7d0b4f..dc3ba29e8ce3 100644 --- a/packages/jest-runtime/src/__tests__/should_instrument.test.js +++ b/packages/jest-runtime/src/__tests__/should_instrument.test.js @@ -6,6 +6,7 @@ * */ +import {normalize} from 'jest-config'; import shouldInstrument from '../shouldInstrument'; describe('shouldInstrument', () => { @@ -13,9 +14,12 @@ describe('shouldInstrument', () => { const defaultOptions = { collectCoverage: true, }; - const defaultConfig = { - rootDir: '/', - }; + const defaultConfig = normalize( + { + rootDir: '/', + }, + {}, + ).options; describe('should return true', () => { const testShouldInstrument = ( @@ -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'], + }); + }); }); }); diff --git a/packages/jest-runtime/src/shouldInstrument.js b/packages/jest-runtime/src/shouldInstrument.js index 6717fbea3e52..6333ece7814e 100644 --- a/packages/jest-runtime/src/shouldInstrument.js +++ b/packages/jest-runtime/src/shouldInstrument.js @@ -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; }