From 13c614425f39b88f4d0a8213da65b352de544ee4 Mon Sep 17 00:00:00 2001 From: H1Gdev Date: Wed, 12 Dec 2018 17:34:24 +0900 Subject: [PATCH 1/2] Fix mistake as test files when run coverage issue. * Check testPathIgnorePatterns. * Add tests. --- .../src/__tests__/should_instrument.test.js | 44 ++++++++++++++++++- .../jest-runtime/src/should_instrument.js | 25 ++++++----- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/packages/jest-runtime/src/__tests__/should_instrument.test.js b/packages/jest-runtime/src/__tests__/should_instrument.test.js index 47d84a96994e..d799ed1e892b 100644 --- a/packages/jest-runtime/src/__tests__/should_instrument.test.js +++ b/packages/jest-runtime/src/__tests__/should_instrument.test.js @@ -27,7 +27,7 @@ describe('should_instrument', () => { expect(result).toBe(true); }; - it('when testRegex provided and file is not a test file', () => { + it('when testRegex is provided and file is not a test file', () => { testShouldInstrument('source_file.js', defaultOptions, { testRegex: ['.*\\.(test)\\.(js)$'], }); @@ -45,6 +45,32 @@ describe('should_instrument', () => { }); }); + it('when testPathIgnorePatterns is provided and file is not a test file', () => { + testShouldInstrument('src/test.js', defaultOptions, { + testPathIgnorePatterns: ['src/'], + }); + }); + + it('when more than one testPathIgnorePatterns is provided and filename is not a test file', () => { + testShouldInstrument('src/test.js', defaultOptions, { + testPathIgnorePatterns: ['test/', 'src/'], + }); + }); + + it('when testRegex and testPathIgnorePatterns are provided and file is not a test file', () => { + testShouldInstrument('src/source_file.js', defaultOptions, { + testPathIgnorePatterns: ['test/'], + testRegex: ['.*\\.(test)\\.(js)$'], + }); + }); + + it('when testMatch and testPathIgnorePatterns are provided and file is not a test file', () => { + testShouldInstrument('src/source_file.js', defaultOptions, { + testMatch: ['**/?(*.)(test).js', '!**/dont/**/*.js'], + testPathIgnorePatterns: ['test/'], + }); + }); + it('should return true when file is in collectCoverageOnlyFrom when provided', () => { testShouldInstrument( 'collect/only/from/here.js', @@ -119,7 +145,7 @@ describe('should_instrument', () => { ); }); - it('when testRegex provided and filename is a test file', () => { + it('when testRegex is provided and filename is a test file', () => { testShouldInstrument(defaultFilename, defaultOptions, { testRegex: ['.*\\.(test)\\.(js)$'], }); @@ -137,6 +163,20 @@ describe('should_instrument', () => { }); }); + it('when testRegex and testPathIgnorePatterns are provided and filename is a test file', () => { + testShouldInstrument('test/' + defaultFilename, defaultOptions, { + testPathIgnorePatterns: ['src/'], + testRegex: ['.*\\.(test)\\.(js)$'], + }); + }); + + it('when testMatch and testPathIgnorePatterns are provided and file is a test file', () => { + testShouldInstrument('test/' + defaultFilename, defaultOptions, { + testMatch: ['**/?(*.)(test).js'], + testPathIgnorePatterns: ['src/'], + }); + }); + it('when file is not in collectCoverageOnlyFrom when provided', () => { testShouldInstrument( 'source_file.js', diff --git a/packages/jest-runtime/src/should_instrument.js b/packages/jest-runtime/src/should_instrument.js index 26189a5a4f0b..2e33d9c5cf35 100644 --- a/packages/jest-runtime/src/should_instrument.js +++ b/packages/jest-runtime/src/should_instrument.js @@ -36,18 +36,23 @@ export default function shouldInstrument( } if ( - config.testRegex && - config.testRegex.some(regex => new RegExp(regex).test(filename)) + !config.testPathIgnorePatterns || + !config.testPathIgnorePatterns.some(pattern => filename.match(pattern)) ) { - return false; - } + if ( + config.testRegex && + config.testRegex.some(regex => new RegExp(regex).test(filename)) + ) { + return false; + } - if ( - config.testMatch && - config.testMatch.length && - micromatch([filename], config.testMatch).length - ) { - return false; + if ( + config.testMatch && + config.testMatch.length && + micromatch([filename], config.testMatch).length + ) { + return false; + } } if ( From f38b0c9d17f92f3e0f144087db6264d585bcbeed Mon Sep 17 00:00:00 2001 From: H1Gdev Date: Wed, 12 Dec 2018 22:15:14 +0900 Subject: [PATCH 2/2] Fix mistake as test files when run coverage issue. * Add #7506 to CHANGELOG. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69eea7572b77..9e9d04e69fdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ - `[jest-haste-map]` Remove legacy condition for duplicate module detection ([#7333](https://github.com/facebook/jest/pull/7333)) - `[jest-haste-map]` Fix `require` detection with trailing commas and ignore `import typeof` modules ([#7385](https://github.com/facebook/jest/pull/7385)) - `[jest-cli]` Fix to set prettierPath via config file ([#7412](https://github.com/facebook/jest/pull/7412)) +- `[jest-runtime]` Fix mistake as test files when run coverage issue. ([#7506](https://github.com/facebook/jest/pull/7506)) ### Chore & Maintenance