Skip to content

Commit

Permalink
Code review: Provide useful message on failed regexp parsing; add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Treworgy committed Oct 19, 2018
1 parent eb8355f commit a2f8e71
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"editor.rulers": [80],
"editor.rulers": [
80
],
"files.exclude": {
"**/.git": true,
"**/node_modules": true,
"**/build": true
},
"editor.formatOnSave": true,
"editor.formatOnSave": false,
"flow.useNPMPackagedFlow": true,
"javascript.validate.enable": false,
"jest.pathToJest": "yarn jest --",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ exports[`testMatch throws if testRegex and testMatch are both specified 1`] = `
<red></>"
`;
exports[`testMatch throws if testRegex is provided an invalid regex string 1`] = `
"<red><bold><bold>● <bold>Validation Error</>:</>
<red></>
<red>Error parsing configuration for <bold>testRegex</>: \\"foo(bar\\" could not be parsed.</>
<red>Error: Invalid regular expression: /foo(bar/: Unterminated group</>
<red></>
<red> <bold>Configuration Documentation:</></>
<red> https://jestjs.io/docs/configuration.html</>
<red></>"
`;
exports[`testPathPattern <regexForTestFiles> ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;
exports[`testPathPattern --testPathPattern ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;
12 changes: 12 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,18 @@ describe('testMatch', () => {
}).toThrowErrorMatchingSnapshot();
});

it('throws if testRegex is provided an invalid regex string', () => {
expect(() => {
normalize(
{
rootDir: '/root',
testRegex: 'foo(bar',
},
{},
);
}).toThrowErrorMatchingSnapshot();
});

it('normalizes testMatch', () => {
const {options} = normalize(
{
Expand Down
14 changes: 13 additions & 1 deletion packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,19 @@ export default function normalize(options: InitialOptions, argv: Argv) {
const valueArray = Array.isArray(valueOrEmptyArray)
? valueOrEmptyArray
: [valueOrEmptyArray];
value = valueArray.map(replacePathSepForRegex).map(e => new RegExp(e));

value = valueArray.map(replacePathSepForRegex).map(pattern => {
try {
return new RegExp(pattern);
} catch (err) {
throw createConfigError(
`Error parsing configuration for ${chalk.bold(
key,
)}: "${pattern}" could not be parsed.\n` +
`Error: ${err.message}`,
);
}
});
break;
case 'moduleFileExtensions': {
value = options[key];
Expand Down

0 comments on commit a2f8e71

Please sign in to comment.