diff --git a/.vscode/settings.json b/.vscode/settings.json index c4951686b125..0b867bdd9628 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 --", diff --git a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap index 19770435b020..ef754afc331b 100644 --- a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap +++ b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap @@ -67,6 +67,17 @@ exports[`testMatch throws if testRegex and testMatch are both specified 1`] = ` " `; +exports[`testMatch throws if testRegex is provided an invalid regex string 1`] = ` +"Validation Error: + +Error parsing configuration for testRegex: \\"foo(bar\\" could not be parsed. +Error: Invalid regular expression: /foo(bar/: Unterminated group + + Configuration Documentation: + https://jestjs.io/docs/configuration.html +" +`; + exports[`testPathPattern ignores invalid regular expressions and logs a warning 1`] = `" Invalid testPattern a( supplied. Running all tests instead."`; exports[`testPathPattern --testPathPattern ignores invalid regular expressions and logs a warning 1`] = `" Invalid testPattern a( supplied. Running all tests instead."`; diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 4d69793652ec..ff45c3036203 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -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( { diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 7db39007304c..0bbd7af6b80b 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -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];