Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling for --config #4230

Merged
merged 3 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import type {Argv} from 'types/Argv';

import {isJSONString} from 'jest-config';
import isCI from 'is-ci';

const check = (argv: Argv) => {
Expand Down Expand Up @@ -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;
};

Expand Down
11 changes: 10 additions & 1 deletion packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,6 +154,7 @@ const getConfigs = (

module.exports = {
getTestEnvironment,
isJSONString,
normalize,
readConfig,
};