diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 74c95cded96b..aacc6265b864 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -10,6 +10,7 @@ import type {Argv} from 'types/Argv'; +import {isJSONString} from 'jest-config'; import isCI from 'is-ci'; const check = (argv: Argv) => { @@ -44,6 +45,17 @@ const check = (argv: Argv) => { ); } + if ( + argv.config && + !isJSONString(argv.config) && + !argv.config.match(/\.js(on)?$/) + ) { + throw new Error( + 'The --config option requires a JSON string literal, or a file path with a .js or .json extension.\n' + + 'Example usage: jest --config ./jest.config.js', + ); + } + return true; }; diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index be077355aa4e..029be103061b 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -36,7 +36,15 @@ function readConfig( // A JSON string was passed to `--config` argument and we can parse it // and use as is. if (isJSONString(argv.config)) { - const config = JSON.parse(argv.config); + let config; + try { + config = JSON.parse(argv.config); + } catch (e) { + throw new Error( + 'There was an error while parsing the `--config` argument as a JSON string.', + ); + } + // NOTE: we might need to resolve this dir to an absolute path in the future config.rootDir = config.rootDir || packageRoot; rawOptions = config; @@ -146,6 +154,7 @@ const getConfigs = ( module.exports = { getTestEnvironment, + isJSONString, normalize, readConfig, };