diff --git a/packages/jest-cli/src/__tests__/cli/args.test.js b/packages/jest-cli/src/__tests__/cli/args.test.js new file mode 100644 index 000000000000..5dc09760e5a5 --- /dev/null +++ b/packages/jest-cli/src/__tests__/cli/args.test.js @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +import type {Argv} from 'types/Argv'; +import {check} from '../../cli/args'; + +describe('check', () => { + it('returns true if the arguments are valid', () => { + const argv: Argv = {}; + expect(check(argv)).toBe(true); + }); + + it('raises an exception if runInBand and maxWorkers are both specified', () => { + const argv: Argv = {maxWorkers: 2, runInBand: true}; + expect(() => check(argv)).toThrow( + 'Both --runInBand and --maxWorkers were specified', + ); + }); + + it('raises an exception if onlyChanged and watchAll are both specified', () => { + const argv: Argv = {onlyChanged: true, watchAll: true}; + expect(() => check(argv)).toThrow( + 'Both --onlyChanged and --watchAll were specified', + ); + }); + + it('raises an exception if findRelatedTests is specified with no file paths', () => { + const argv: Argv = {_: [], findRelatedTests: true}; + expect(() => check(argv)).toThrow( + 'The --findRelatedTests option requires file paths to be specified', + ); + }); + + it('raises an exception if maxWorkers is specified with no number', () => { + const argv: Argv = {maxWorkers: undefined}; + expect(() => check(argv)).toThrow( + 'The --maxWorkers (-w) option requires a number to be specified', + ); + }); + + it('raises an exception if config is not a valid JSON string', () => { + const argv: Argv = {config: 'x:1'}; + expect(() => check(argv)).toThrow( + 'The --config option requires a JSON string literal, or a file path with a .js or .json extension', + ); + }); +}); diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index f14799bec760..f045fce760fc 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -36,6 +36,14 @@ export const check = (argv: Argv) => { ); } + if (argv.hasOwnProperty('maxWorkers') && argv.maxWorkers === undefined) { + throw new Error( + 'The --maxWorkers (-w) option requires a number to be specified.\n' + + 'Example usage: jest --maxWorkers 2\n' + + 'Or did you mean --watch?', + ); + } + if ( argv.config && !isJSONString(argv.config) &&