From 3c753bf4d6e07a9256f3b36c14529b9505e27f15 Mon Sep 17 00:00:00 2001 From: Justin Bay Date: Tue, 13 Feb 2018 22:22:41 -0500 Subject: [PATCH 1/2] Replace path sep for testRegex in should_instrument so test files don't get instrumented on windows given a pattern like /__tests__/*.js --- CHANGELOG.md | 6 ++++++ integration-tests/__tests__/coverage_remapping.test.js | 3 --- packages/jest-runtime/src/should_instrument.js | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7fb6bae16e4..7c469000f8e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## master +### Fixes + +* `[jest-runtime]` Replace path sep for testRegex in should_instrument so test +files don't get instrumented on windows given a pattern like /__tests__/*.js + ([#5560](https://github.com/facebook/jest/pull/5560)) + ## jest 22.3.0 ### Fixes diff --git a/integration-tests/__tests__/coverage_remapping.test.js b/integration-tests/__tests__/coverage_remapping.test.js index 390a14a9a135..bf31e5dae024 100644 --- a/integration-tests/__tests__/coverage_remapping.test.js +++ b/integration-tests/__tests__/coverage_remapping.test.js @@ -11,15 +11,12 @@ const {readFileSync} = require('fs'); const path = require('path'); -const SkipOnWindows = require('../../scripts/SkipOnWindows'); const {cleanup, run} = require('../utils'); const runJest = require('../runJest'); const dir = path.resolve(__dirname, '../coverage-remapping'); const coverageDir = path.join(dir, 'coverage'); -SkipOnWindows.suite(); - beforeAll(() => { cleanup(coverageDir); }); diff --git a/packages/jest-runtime/src/should_instrument.js b/packages/jest-runtime/src/should_instrument.js index 0a663ee56378..a3ea47185f0b 100644 --- a/packages/jest-runtime/src/should_instrument.js +++ b/packages/jest-runtime/src/should_instrument.js @@ -11,7 +11,7 @@ import type {Path, ProjectConfig} from 'types/Config'; import type {Options} from './script_transformer'; import path from 'path'; -import {escapePathForRegex} from 'jest-regex-util'; +import {escapePathForRegex, replacePathSepForRegex} from 'jest-regex-util'; import micromatch from 'micromatch'; const MOCKS_PATTERN = new RegExp( @@ -35,7 +35,10 @@ export default function shouldInstrument( return true; } - if (config.testRegex && filename.match(config.testRegex)) { + if ( + config.testRegex && + new RegExp(replacePathSepForRegex(config.testRegex)).test(filename) + ) { return false; } From ef4a81c3c27f494533971e150e37b48e5fae2961 Mon Sep 17 00:00:00 2001 From: Justin Bay Date: Wed, 14 Feb 2018 19:31:30 -0500 Subject: [PATCH 2/2] move testRegex normalization to jest-config --- CHANGELOG.md | 4 ++-- packages/jest-cli/src/search_source.js | 6 ++---- packages/jest-config/src/normalize.js | 4 +++- packages/jest-runtime/src/should_instrument.js | 7 ++----- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c469000f8e2..934b09d18067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ### Fixes -* `[jest-runtime]` Replace path sep for testRegex in should_instrument so test -files don't get instrumented on windows given a pattern like /__tests__/*.js +* `[jest-runtime]` Align handling of testRegex on Windows between searching for + tests and instrumentation checks ([#5560](https://github.com/facebook/jest/pull/5560)) ## jest 22.3.0 diff --git a/packages/jest-cli/src/search_source.js b/packages/jest-cli/src/search_source.js index 6f153467be58..efb39ba054a0 100644 --- a/packages/jest-cli/src/search_source.js +++ b/packages/jest-cli/src/search_source.js @@ -17,7 +17,7 @@ import path from 'path'; import micromatch from 'micromatch'; import DependencyResolver from 'jest-resolve-dependencies'; import testPathPatternToRegExp from './test_path_pattern_to_regexp'; -import {escapePathForRegex, replacePathSepForRegex} from 'jest-regex-util'; +import {escapePathForRegex} from 'jest-regex-util'; type SearchResult = {| noSCM?: boolean, @@ -36,8 +36,6 @@ export type TestSelectionConfig = {| watch?: boolean, |}; -const pathToRegex = p => replacePathSepForRegex(p); - const globsToMatcher = (globs: ?Array) => { if (globs == null || globs.length === 0) { return () => true; @@ -52,7 +50,7 @@ const regexToMatcher = (testRegex: string) => { return () => true; } - const regex = new RegExp(pathToRegex(testRegex)); + const regex = new RegExp(testRegex); return path => regex.test(path); }; diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 8a7acc71e365..77f91ff48aa3 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -463,6 +463,9 @@ export default function normalize(options: InitialOptions, argv: Argv) { options[key], ); break; + case 'testRegex': + value = options[key] && replacePathSepForRegex(options[key]); + break; case 'automock': case 'bail': case 'browser': @@ -506,7 +509,6 @@ export default function normalize(options: InitialOptions, argv: Argv) { case 'testFailureExitCode': case 'testLocationInResults': case 'testNamePattern': - case 'testRegex': case 'testURL': case 'timers': case 'useStderr': diff --git a/packages/jest-runtime/src/should_instrument.js b/packages/jest-runtime/src/should_instrument.js index a3ea47185f0b..0a663ee56378 100644 --- a/packages/jest-runtime/src/should_instrument.js +++ b/packages/jest-runtime/src/should_instrument.js @@ -11,7 +11,7 @@ import type {Path, ProjectConfig} from 'types/Config'; import type {Options} from './script_transformer'; import path from 'path'; -import {escapePathForRegex, replacePathSepForRegex} from 'jest-regex-util'; +import {escapePathForRegex} from 'jest-regex-util'; import micromatch from 'micromatch'; const MOCKS_PATTERN = new RegExp( @@ -35,10 +35,7 @@ export default function shouldInstrument( return true; } - if ( - config.testRegex && - new RegExp(replacePathSepForRegex(config.testRegex)).test(filename) - ) { + if (config.testRegex && filename.match(config.testRegex)) { return false; }