diff --git a/CHANGELOG.md b/CHANGELOG.md index 9696b0bc75d3..bf56b3a9e0d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - `[jest-circus]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372)) - `[jest-cli]` Fix unhandled error when a bad revision is provided to `changedSince` ([#7115](https://github.com/facebook/jest/pull/7115)) - `[jest-config]` Moved dynamically assigned `cwd` from `jest-cli` to default configuration in `jest-config` ([#7146](https://github.com/facebook/jest/pull/7146)) +- `[jest-config]` Fix `getMaxWorkers` on termux ([#7154](https://github.com/facebook/jest/pull/7154)) ### Chore & Maintenance diff --git a/packages/jest-config/src/__mocks__/os.js b/packages/jest-config/src/__mocks__/os.js new file mode 100644 index 000000000000..371c0caa36ed --- /dev/null +++ b/packages/jest-config/src/__mocks__/os.js @@ -0,0 +1,20 @@ +/** + * 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'; + +const os = jest.genMockFromModule('os'); + +let cpus; +function __setCpus(newCpus) { + cpus = newCpus; +} + +os.__setCpus = __setCpus; +os.cpus = jest.fn(() => cpus); + +module.exports = os; diff --git a/packages/jest-config/src/__tests__/getMaxWorkers.test.js b/packages/jest-config/src/__tests__/getMaxWorkers.test.js index 0874d10dc7ef..5b0d9fb26b95 100644 --- a/packages/jest-config/src/__tests__/getMaxWorkers.test.js +++ b/packages/jest-config/src/__tests__/getMaxWorkers.test.js @@ -8,16 +8,23 @@ import getMaxWorkers from '../getMaxWorkers'; -jest.mock('os', () => ({ - cpus: () => ({length: 4}), -})); +jest.mock('os'); describe('getMaxWorkers', () => { + beforeEach(() => { + require('os').__setCpus({length: 4}); + }); + it('Returns 1 when runInBand', () => { const argv = {runInBand: true}; expect(getMaxWorkers(argv)).toBe(1); }); + it('Returns 1 when the OS CPUs are not available', () => { + require('os').__setCpus(undefined); + expect(getMaxWorkers({})).toBe(1); + }); + it('Returns the `maxWorkers` when specified', () => { const argv = {maxWorkers: 8}; expect(getMaxWorkers(argv)).toBe(8); diff --git a/packages/jest-config/src/getMaxWorkers.js b/packages/jest-config/src/getMaxWorkers.js index 557f0ed6a55a..307ef02c9653 100644 --- a/packages/jest-config/src/getMaxWorkers.js +++ b/packages/jest-config/src/getMaxWorkers.js @@ -17,7 +17,7 @@ export default function getMaxWorkers(argv: Argv): number { } else if (argv.maxWorkers) { return parseInt(argv.maxWorkers, 10); } else { - const cpus = os.cpus().length; + const cpus = os.cpus() ? os.cpus().length : 1; return Math.max(argv.watch ? Math.floor(cpus / 2) : cpus - 1, 1); } }