Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getMaxWorkers on termux #7154

Merged
merged 2 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}