Skip to content

Commit

Permalink
Fix handling regex symbols in tests path on Windows (#5941)
Browse files Browse the repository at this point in the history
  • Loading branch information
hron authored and thymikee committed Apr 9, 2018
1 parent 6979b8e commit aa9790d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
([#5720](https://github.com/facebook/jest/pull/5720))
* `[pretty-format]` Handle React fragments better
([#5816](https://github.com/facebook/jest/pull/5816))
* `[jest-regex-util]` Fix handling regex symbols in tests path on Windows
([#5941](https://github.com/facebook/jest/pull/5941))

### Chore & Maintenance

Expand Down
3 changes: 0 additions & 3 deletions integration-tests/__tests__/regex_(char_in_path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
*/
'use strict';

const SkipOnWindows = require('../../scripts/SkipOnWindows');
const runJest = require('../runJest');

describe('Regex Char In Path', () => {
SkipOnWindows.suite();

it('parses paths containing regex chars correctly', () => {
const {json} = runJest.json('regex-(char-in-path', []);

Expand Down
4 changes: 4 additions & 0 deletions packages/jest-regex-util/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ describe('replacePathSepForRegex()', () => {
'a\\\\\\\\\\.dotfile',
);
});

it('should not escape an escaped regexp symbol', () => {
expect(replacePathSepForRegex('b\\(86')).toBe('b\\(86');
});
});
});
2 changes: 1 addition & 1 deletion packages/jest-regex-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const escapeStrForRegex = (string: string) =>

export const replacePathSepForRegex = (string: string) => {
if (path.sep === '\\') {
return string.replace(/(\/|\\(?!\.))/g, '\\\\');
return string.replace(/(\/|\\(?![[\]{}()*+?.^$|]))/g, '\\\\');
}
return string;
};

3 comments on commit aa9790d

@turnerhayes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems to have broken (for Windows) the regex that create-react-app generates for transformIgnorePatterns: that regex is '[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$' see this line, which, when run through this function, yields: [\\\]node_modules[\\\].+\\.(js|jsx|mjs)$. This is an invalid regex, because it escapes the closing ] characters and the character class is never terminated.

This can be fixed by changing it to /node_modules/.+\\.(js|jsx|mjs)$, which is nice, but it's unfortunate that create-react-app is broken by this.

See this regex playground.

@SimenB
Copy link
Member

@SimenB SimenB commented on aa9790d Jun 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #6385

@turnerhayes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah great, I didn't see that, thanks!

Please sign in to comment.