diff --git a/CHANGELOG.md b/CHANGELOG.md index 865d3a5fca39..32a04647a3bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#6398](https://github.com/facebook/jest/pull/6398)) - `[jest-util]` `console.timeEnd` now properly log elapsed time in milliseconds. ([#6456](https://github.com/facebook/jest/pull/6456)) - `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505)) +- `[jest-regex-util]` Improve handling already escaped path separators on Windows ([#6523](https://github.com/facebook/jest/pull/6523)) ### Chore & Maintenance diff --git a/packages/jest-regex-util/src/__tests__/index.test.js b/packages/jest-regex-util/src/__tests__/index.test.js index a15eaa77d1e6..7fd740e468bc 100644 --- a/packages/jest-regex-util/src/__tests__/index.test.js +++ b/packages/jest-regex-util/src/__tests__/index.test.js @@ -31,17 +31,19 @@ describe('replacePathSepForRegex()', () => { it('should not escape an escaped dot', () => { expect(replacePathSepForRegex('a\\.dotfile')).toBe('a\\.dotfile'); - - // If we expect Windows path separators to be escaped, one would expect - // the regular expression "\\\." to be unescaped as "\.". This is not the - // current behavior. - expect(replacePathSepForRegex('a\\\\\\.dotfile')).toBe( - 'a\\\\\\\\\\.dotfile', - ); + expect(replacePathSepForRegex('a\\\\\\.dotfile')).toBe('a\\\\\\.dotfile'); }); it('should not escape an escaped regexp symbol', () => { expect(replacePathSepForRegex('b\\(86')).toBe('b\\(86'); }); + + it('should escape Windows path separators inside groups', () => { + expect(replacePathSepForRegex('[/\\\\]')).toBe('[\\\\\\\\]'); + }); + + it('should not escape several already escaped path separators', () => { + expect(replacePathSepForRegex('\\\\\\\\')).toBe('\\\\\\\\'); + }); }); }); diff --git a/packages/jest-regex-util/src/index.js b/packages/jest-regex-util/src/index.js index 635a1931f0ff..ee16071c8107 100644 --- a/packages/jest-regex-util/src/index.js +++ b/packages/jest-regex-util/src/index.js @@ -23,7 +23,10 @@ export const escapeStrForRegex = (string: string) => export const replacePathSepForRegex = (string: string) => { if (path.sep === '\\') { - return string.replace(/(\/|\\(?![[\]{}()*+?.^$|]))/g, '\\\\'); + return string.replace( + /(\/|(.)\\(?![[\]{}()*+?.^$|\\]))/g, + (_match, p1, p2) => (p2 && p2 !== '\\' ? p2 + '\\\\' : '\\\\'), + ); } return string; };