diff --git a/CHANGELOG.md b/CHANGELOG.md index c62ad76c6881..9282dc4b02e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - `[jest-runtime]` Fix module registry memory leak ([#8282](https://github.com/facebook/jest/pull/8282)) - `[jest-resolve]` optimize resolve module path ([#8388](https://github.com/facebook/jest/pull/8388)) +- `[jest-config]` Adjust `--maxWorkers` behavior to use the lesser of provided number and number of cores. ([#8410](https://github.com/facebook/jest/pull/8410)) ## 24.7.1 diff --git a/packages/jest-config/src/__tests__/getMaxWorkers.test.ts b/packages/jest-config/src/__tests__/getMaxWorkers.test.ts index aab3290c5357..71196f33b6ca 100644 --- a/packages/jest-config/src/__tests__/getMaxWorkers.test.ts +++ b/packages/jest-config/src/__tests__/getMaxWorkers.test.ts @@ -25,9 +25,9 @@ describe('getMaxWorkers', () => { expect(getMaxWorkers({})).toBe(1); }); - it('Returns the `maxWorkers` when specified', () => { - const argv = {maxWorkers: 8}; - expect(getMaxWorkers(argv)).toBe(8); + it('Returns the lesser of `maxWorkers` and (the number of cpus - 1)', () => { + expect(getMaxWorkers({maxWorkers: 2})).toBe(2); + expect(getMaxWorkers({maxWorkers: 50})).toBe(3); }); it('Returns based on the number of cpus', () => { diff --git a/packages/jest-config/src/getMaxWorkers.ts b/packages/jest-config/src/getMaxWorkers.ts index b96024477d6b..aa95e77bf421 100644 --- a/packages/jest-config/src/getMaxWorkers.ts +++ b/packages/jest-config/src/getMaxWorkers.ts @@ -11,6 +11,7 @@ import {Config} from '@jest/types'; export default function getMaxWorkers( argv: Partial>, ): number { + const cpus = os.cpus() ? os.cpus().length : 1; if (argv.runInBand) { return 1; } else if (argv.maxWorkers) { @@ -24,15 +25,13 @@ export default function getMaxWorkers( parsed > 0 && parsed <= 100 ) { - const cpus = os.cpus().length; const workers = Math.floor((parsed / 100) * cpus); return workers >= 1 ? workers : 1; } - return parsed > 0 ? parsed : 1; + return Math.max(Math.min(cpus - 1, parsed), 1); } else { // In watch mode, Jest should be unobtrusive and not use all available CPUs. - const cpus = os.cpus() ? os.cpus().length : 1; return Math.max(argv.watch ? Math.floor(cpus / 2) : cpus - 1, 1); } } diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 7748d2cb2439..dff0dd3fae13 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -573,7 +573,7 @@ const calcIgnorePatternRegExp = (config: Config.ProjectConfig) => { !config.transformIgnorePatterns || config.transformIgnorePatterns.length === 0 ) { - return; + return undefined; } return new RegExp(config.transformIgnorePatterns.join('|')); @@ -581,7 +581,7 @@ const calcIgnorePatternRegExp = (config: Config.ProjectConfig) => { const calcTransformRegExp = (config: Config.ProjectConfig) => { if (!config.transform.length) { - return; + return undefined; } const transformRegexp: Array<[RegExp, string]> = [];