Skip to content

Commit

Permalink
Fix getMaxWorkers on termux (#7154)
Browse files Browse the repository at this point in the history
  • Loading branch information
rplopes authored and SimenB committed Oct 12, 2018
1 parent d45fdb9 commit a30b83e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions packages/jest-config/src/__mocks__/os.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 10 additions & 3 deletions packages/jest-config/src/__tests__/getMaxWorkers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/getMaxWorkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a30b83e

Please sign in to comment.