From 7c5578035d469add0a8495c30ad9a911f18e7ffd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 6 Feb 2019 13:44:24 +0100 Subject: [PATCH 1/3] fix: normalize path separator for rootDir replacement --- packages/jest-config/package.json | 3 +- .../src/__tests__/normalize.test.js | 57 ++++++++++++++----- packages/jest-config/src/utils.js | 9 ++- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index c1dc8cde0f46..b0834b9e2674 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -23,7 +23,8 @@ "jest-validate": "^24.0.0", "micromatch": "^3.1.10", "pretty-format": "^24.0.0", - "realpath-native": "^1.0.2" + "realpath-native": "^1.0.2", + "slash": "^2.0.0" }, "devDependencies": { "@types/babel__core": "^7.0.4", diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index e1e24827dade..36917667433b 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -9,7 +9,6 @@ import crypto from 'crypto'; import path from 'path'; import {escapeStrForRegex} from 'jest-regex-util'; -import normalize from '../normalize'; import Defaults from '../Defaults'; import {DEFAULT_JS_PATTERN} from '../constants'; @@ -18,6 +17,8 @@ const DEFAULT_CSS_PATTERN = '^.+\\.(css)$'; jest.mock('jest-resolve').mock('path', () => jest.requireActual('path').posix); +let normalize; + let root; let expectedPathFooBar; let expectedPathFooQux; @@ -39,6 +40,7 @@ function joinForPattern() { } beforeEach(() => { + normalize = require('../normalize').default; root = path.resolve('/'); expectedPathFooBar = path.join(root, 'root', 'path', 'foo', 'bar', 'baz'); expectedPathFooQux = path.join(root, 'root', 'path', 'foo', 'qux', 'quux'); @@ -924,6 +926,39 @@ describe('moduleDirectories', () => { '/root/node_modules', ]); }); + + describe('win32', () => { + beforeEach(() => { + jest.resetModules(); + jest.mock('path', () => jest.requireActual('path').win32); + + normalize = require('../normalize').default; + require('jest-resolve').findNodeModule = findNodeModule; + }); + + afterEach(() => { + jest.resetModules(); + jest.mock('path', () => jest.requireActual('path').posix); + + normalize = require('../normalize').default; + }); + + it('correctly handles globs', () => { + const {options} = normalize( + { + rootDir: '/root', + testMatch: [ + '/test/{TestCasesNormal,StatsTestCases,ConfigTestCases}.test.js', + ], + }, + {}, + ); + + expect(options.testMatch).toEqual([ + '/root/test/{TestCasesNormal,StatsTestCases,ConfigTestCases}.test.js', + ]); + }); + }); }); describe('preset', () => { @@ -1375,40 +1410,36 @@ describe('testPathPattern', () => { describe('win32', () => { beforeEach(() => { + jest.resetModules(); jest.mock('path', () => jest.requireActual('path').win32); + normalize = require('../normalize').default; require('jest-resolve').findNodeModule = findNodeModule; }); afterEach(() => { jest.resetModules(); + jest.mock('path', () => jest.requireActual('path').posix); + normalize = require('../normalize').default; + require('jest-resolve').findNodeModule = findNodeModule; }); it('preserves any use of "\\"', () => { const argv = {[opt.property]: ['a\\b', 'c\\\\d']}; - const {options} = require('../normalize').default( - initialOptions, - argv, - ); + const {options} = normalize(initialOptions, argv); expect(options.testPathPattern).toBe('a\\b|c\\\\d'); }); it('replaces POSIX path separators', () => { const argv = {[opt.property]: ['a/b']}; - const {options} = require('../normalize').default( - initialOptions, - argv, - ); + const {options} = normalize(initialOptions, argv); expect(options.testPathPattern).toBe('a\\\\b'); }); it('replaces POSIX paths in multiple args', () => { const argv = {[opt.property]: ['a/b', 'c/d']}; - const {options} = require('../normalize').default( - initialOptions, - argv, - ); + const {options} = normalize(initialOptions, argv); expect(options.testPathPattern).toBe('a\\\\b|c\\\\d'); }); diff --git a/packages/jest-config/src/utils.js b/packages/jest-config/src/utils.js index 168c07657cb3..da2b32f4b971 100644 --- a/packages/jest-config/src/utils.js +++ b/packages/jest-config/src/utils.js @@ -13,6 +13,7 @@ import path from 'path'; import {ValidationError} from 'jest-validate'; import Resolver from 'jest-resolve'; import chalk from 'chalk'; +import slash from 'slash'; type ResolveOptions = {| rootDir: string, @@ -66,9 +67,11 @@ export const replaceRootDirInPath = ( return filePath; } - return path.resolve( - rootDir, - path.normalize('./' + filePath.substr(''.length)), + return slash( + path.resolve( + rootDir, + path.normalize('./' + filePath.substr(''.length)), + ), ); }; From 54adc360607ffc3789acd5bb018910266f035ae8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 6 Feb 2019 13:46:22 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d039d87a4441..5a61297c5a55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-config]` fix: normalize path separator for rootDir replacement ([#7814](https://github.com/facebook/jest/pull/7814)) + ### Chore & Maintenance - `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808)) From d81c4cb5443e529a5c0572e5b9e4f231249c3609 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 6 Feb 2019 13:50:17 +0100 Subject: [PATCH 3/3] add types as well --- packages/jest-config/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index b0834b9e2674..5cbf7d636313 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -29,7 +29,8 @@ "devDependencies": { "@types/babel__core": "^7.0.4", "@types/glob": "^7.1.1", - "@types/micromatch": "^3.1.0" + "@types/micromatch": "^3.1.0", + "@types/slash": "^2.0.0" }, "engines": { "node": ">= 6"