Skip to content

Commit

Permalink
Check if the maxWorkers has a value and raises an exception otherwise (
Browse files Browse the repository at this point in the history
…#4591)

* 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

* Add expected exception message in the check method tests

* Improve error message for when maxWorkers is undefined
  • Loading branch information
nicolasiensen authored and cpojer committed Oct 3, 2017
1 parent 7aa3017 commit 1cda2e2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/jest-cli/src/__tests__/cli/args.test.js
Original file line number Diff line number Diff line change
@@ -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',
);
});
});
8 changes: 8 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down

0 comments on commit 1cda2e2

Please sign in to comment.