From f48c6be712067ef410240b99ecfbe52c045f58a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcolas=20Iensen?= Date: Tue, 3 Oct 2017 11:14:39 +0200 Subject: [PATCH 1/3] Check if the maxWorkers has a value and raises an exception otherwise In this commit we are adding a new validation in the check method of the jest-cli package, so we raise an exception if the user tries to run Jest with undefined maxWorkers, e.g. jest --maxWorkers jest --maxWorkers 2 Some users were confusing -w as an alias to --watch, but in fact it is an alias to --maxWorkers. This change will also make this distinction clearer. Fixes issue #4577 --- .../jest-cli/src/__tests__/cli/args.test.js | 44 +++++++++++++++++++ packages/jest-cli/src/cli/args.js | 8 ++++ 2 files changed, 52 insertions(+) create mode 100644 packages/jest-cli/src/__tests__/cli/args.test.js 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..e8e6de953909 --- /dev/null +++ b/packages/jest-cli/src/__tests__/cli/args.test.js @@ -0,0 +1,44 @@ +/** + * 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(); + }); + + it('raises an exception if onlyChanged and watchAll are both specified', () => { + const argv: Argv = {onlyChanged: true, watchAll: true}; + expect(() => check(argv)).toThrow(); + }); + + it('raises an exception if findRelatedTests is specified with no file paths', () => { + const argv: Argv = {findRelatedTests: true}; + expect(() => check(argv)).toThrow(); + }); + + it('raises an exception if maxWorkers is specified with no number', () => { + const argv: Argv = {maxWorkers: undefined}; + expect(() => check(argv)).toThrow(); + }); + + it('raises an exception if config is not a valid JSON string', () => { + const argv: Argv = {config: 'x:1'}; + expect(() => check(argv)).toThrow(); + }); +}); diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index f14799bec760..709eb5063c25 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 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) && From 07c06743fc69fbb99fcb56eedc8febdba5a455cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcolas=20Iensen?= Date: Tue, 3 Oct 2017 11:47:44 +0200 Subject: [PATCH 2/3] Add expected exception message in the check method tests --- .../jest-cli/src/__tests__/cli/args.test.js | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/jest-cli/src/__tests__/cli/args.test.js b/packages/jest-cli/src/__tests__/cli/args.test.js index e8e6de953909..ca04529e1d5e 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.js +++ b/packages/jest-cli/src/__tests__/cli/args.test.js @@ -19,26 +19,36 @@ describe('check', () => { it('raises an exception if runInBand and maxWorkers are both specified', () => { const argv: Argv = {maxWorkers: 2, runInBand: true}; - expect(() => check(argv)).toThrow(); + 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(); + 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(); + 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(); + expect(() => check(argv)).toThrow( + 'The --maxWorkers 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(); + expect(() => check(argv)).toThrow( + 'The --config option requires a JSON string literal, or a file path with a .js or .json extension', + ); }); }); From 2ff0f6b4fad6ecc89c176a7aa586de00b434c403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcolas=20Iensen?= Date: Tue, 3 Oct 2017 11:48:56 +0200 Subject: [PATCH 3/3] Improve error message for when maxWorkers is undefined --- packages/jest-cli/src/__tests__/cli/args.test.js | 2 +- packages/jest-cli/src/cli/args.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-cli/src/__tests__/cli/args.test.js b/packages/jest-cli/src/__tests__/cli/args.test.js index ca04529e1d5e..5dc09760e5a5 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.js +++ b/packages/jest-cli/src/__tests__/cli/args.test.js @@ -41,7 +41,7 @@ describe('check', () => { it('raises an exception if maxWorkers is specified with no number', () => { const argv: Argv = {maxWorkers: undefined}; expect(() => check(argv)).toThrow( - 'The --maxWorkers option requires a number to be specified', + 'The --maxWorkers (-w) option requires a number to be specified', ); }); diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 709eb5063c25..f045fce760fc 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -38,9 +38,9 @@ export const check = (argv: Argv) => { if (argv.hasOwnProperty('maxWorkers') && argv.maxWorkers === undefined) { throw new Error( - 'The --maxWorkers option requires a number to be specified.\n' + + 'The --maxWorkers (-w) option requires a number to be specified.\n' + 'Example usage: jest --maxWorkers 2\n' + - 'Or did you mean --watch ?', + 'Or did you mean --watch?', ); }