Skip to content

Commit

Permalink
Fix getMaxWorkers on termux
Browse files Browse the repository at this point in the history
  • Loading branch information
rplopes committed Oct 12, 2018
1 parent 344afc6 commit b589628
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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 b589628

Please sign in to comment.