Skip to content

Commit

Permalink
Allow % based configuration of max workers (jestjs#7494)
Browse files Browse the repository at this point in the history
* Allow % based configuration of max workers

* Add pull ref

* Whoops, copy and paste

* Update docs/CLI.md

Co-Authored-By: phawxby <phawxby@gmail.com>

* Move changlog entry
  • Loading branch information
phawxby authored and captain-yossarian committed Jul 18, 2019
1 parent 63085c2 commit e2e88c6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` Treats `Infinity` as equal in toBeCloseTo matcher ([#7405](https://github.com/facebook/jest/pull/7405))
- `[jest-worker]` Add node worker-thread support to jest-worker ([#7408](https://github.com/facebook/jest/pull/7408))
- `[jest-config]` Allow `bail` setting to be configured with a number allowing tests to abort after `n` of failures ([#7335](https://github.com/facebook/jest/pull/7335))
- `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494))

### Fixes

Expand Down
4 changes: 3 additions & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ Lists all tests as JSON that Jest will run given the arguments, and exits. This

Logs the heap usage after every test. Useful to debug memory leaks. Use together with `--runInBand` and `--expose-gc` in node.

### `--maxWorkers=<num>`
### `--maxWorkers=<num>|<string>`

Alias: `-w`. Specifies the maximum number of workers the worker-pool will spawn for running tests. This defaults to the number of the cores available on your machine. It may be useful to adjust this in resource limited environments like CIs but the default should be adequate for most use-cases.

For environments with variable CPUs available, you can use percentage based configuration: `--maxWorkers=50%`

### `--noStackTrace`

Disables stack trace in test results output.
Expand Down
17 changes: 17 additions & 0 deletions packages/jest-config/src/__tests__/getMaxWorkers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ describe('getMaxWorkers', () => {
expect(getMaxWorkers({})).toBe(3);
expect(getMaxWorkers({watch: true})).toBe(2);
});

describe('% based', () => {
it('50% = 2 workers', () => {
const argv = {maxWorkers: '50%'};
expect(getMaxWorkers(argv)).toBe(2);
});

it('< 0 workers should become 1', () => {
const argv = {maxWorkers: '1%'};
expect(getMaxWorkers(argv)).toBe(1);
});

it("0% shouldn't break", () => {
const argv = {maxWorkers: '0%'};
expect(getMaxWorkers(argv)).toBe(1);
});
});
});
15 changes: 14 additions & 1 deletion packages/jest-config/src/getMaxWorkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ export default function getMaxWorkers(argv: Argv): number {
if (argv.runInBand) {
return 1;
} else if (argv.maxWorkers) {
return parseInt(argv.maxWorkers, 10);
const parsed = parseInt(argv.maxWorkers, 10);

if (
typeof argv.maxWorkers === 'string' &&
argv.maxWorkers.trim().endsWith('%') &&
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;
} else {
const cpus = os.cpus() ? os.cpus().length : 1;
return Math.max(argv.watch ? Math.floor(cpus / 2) : cpus - 1, 1);
Expand Down

0 comments on commit e2e88c6

Please sign in to comment.