Skip to content

Commit

Permalink
Fix "should_instrument" decision when given negative testMatch pattern (
Browse files Browse the repository at this point in the history
#7170)

## Summary

Files that match with the `testFiles` configuration aren't instrumented for test coverage.

This check was being done with `multimatch.any` which behaves unexpectedly when given an inverse glob pattern (`!**/dont/**/*.js`).

Reproduction: https://github.com/g-harel/jest-7165 
Fixes #7165

## Test plan

<!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes UI. -->

I've modified an existing test case to verify that the fix works.

I also linked my local fork to the reproduction project to confirm that the change had the intended effect.
  • Loading branch information
g-harel authored and SimenB committed Oct 16, 2018
1 parent 87a5a67 commit ec87f3d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- `[jest-config]` Moved dynamically assigned `cwd` from `jest-cli` to default configuration in `jest-config` ([#7146](https://github.com/facebook/jest/pull/7146))
- `[jest-config]` Fix `getMaxWorkers` on termux ([#7154](https://github.com/facebook/jest/pull/7154))
- `[jest-runtime]` Throw an explicit error if `js` is missing from `moduleFileExtensions` ([#7160](https://github.com/facebook/jest/pull/7160))
- `[jest-runtime]` Fix missing coverage when using negative glob pattern in `testMatch` ([#7170](https://github.com/facebook/jest/pull/7170))

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ describe('should_instrument', () => {

it('when testMatch is provided and file is not a test file', () => {
testShouldInstrument('source_file.js', defaultOptions, {
testMatch: ['**/?(*.)(test).js'],
testMatch: ['**/?(*.)(test).js', '!**/dont/**/*.js'],
});
});

it('should return true when file is in collectCoverageOnlyFrom when provided', () => {
testShouldInstrument(
'collect/only/from/here.js',

{
collectCoverage: true,
collectCoverageOnlyFrom: {'collect/only/from/here.js': true},
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/should_instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function shouldInstrument(
if (
config.testMatch &&
config.testMatch.length &&
micromatch.any(filename, config.testMatch)
micromatch([filename], config.testMatch).length
) {
return false;
}
Expand Down

0 comments on commit ec87f3d

Please sign in to comment.