Skip to content

Commit be9b9f3

Browse files
benjaminjkraftDmitryMakhnev
authored andcommitted
fix(jest-core): always use workers in watch mode (jestjs#14059)
1 parent 9d73f80 commit be9b9f3

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Fixes
88

99
- `[jest-config]` Handle frozen config object ([#14054](https://github.com/facebook/jest/pull/14054))
10+
- `[jest-core]` Always use workers in watch mode to avoid crashes ([#14059](https://github.com/facebook/jest/pull/14059)).
1011
- `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989))
1112
- `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007))
1213
- `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008))

packages/jest-core/src/__tests__/testSchedulerHelper.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const getTestsMock = () => [getTestMock(), getTestMock()];
2424

2525
test.each`
2626
tests | timings | detectOpenHandles | maxWorkers | watch | workerIdleMemoryLimit | expectedResult
27-
${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${undefined} | ${true}
28-
${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${undefined} | ${true}
27+
${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${undefined} | ${false}
28+
${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${undefined} | ${false}
2929
${getTestsMock()} | ${[2000, 500]} | ${false} | ${2} | ${true} | ${undefined} | ${false}
3030
${[getTestMock()]} | ${[2000, 500]} | ${false} | ${undefined} | ${true} | ${undefined} | ${false}
3131
${getTestMock()} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${undefined} | ${false}
@@ -36,8 +36,8 @@ test.each`
3636
${new Array(45)} | ${[500]} | ${false} | ${undefined} | ${false} | ${undefined} | ${false}
3737
${getTestsMock()} | ${[2000, 500]} | ${false} | ${undefined} | ${false} | ${undefined} | ${false}
3838
${getTestsMock()} | ${[2000, 500]} | ${true} | ${undefined} | ${false} | ${undefined} | ${true}
39-
${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${'500MB'} | ${true}
40-
${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${'500MB'} | ${true}
39+
${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${'500MB'} | ${false}
40+
${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${'500MB'} | ${false}
4141
${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${false} | ${'500MB'} | ${false}
4242
${[getTestMock()]} | ${[2000]} | ${false} | ${undefined} | ${false} | ${'500MB'} | ${false}
4343
${getTestsMock()} | ${[500, 500]} | ${false} | ${undefined} | ${false} | ${'500MB'} | ${false}

packages/jest-core/src/testSchedulerHelper.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,26 @@ export function shouldRunInBand(
2727
}
2828

2929
/*
30-
* Run in band if we only have one test or one worker available, unless we
31-
* are using the watch mode, in which case the TTY has to be responsive and
32-
* we cannot schedule anything in the main thread. Same logic applies to
33-
* watchAll.
30+
* If we are using watch/watchAll mode, don't schedule anything in the main
31+
* thread to keep the TTY responsive and to prevent watch mode crashes caused
32+
* by leaks (improper test teardown).
33+
*/
34+
if (watch || watchAll) {
35+
return false;
36+
}
37+
38+
/*
39+
* Otherwise, run in band if we only have one test or one worker available.
3440
* Also, if we are confident from previous runs that the tests will finish
3541
* quickly we also run in band to reduce the overhead of spawning workers.
3642
* Finally, the user can provide the runInBand argument in the CLI to
37-
* force running in band.
38-
* https://github.com/facebook/jest/blob/700e0dadb85f5dc8ff5dac6c7e98956690049734/packages/jest-config/src/getMaxWorkers.js#L14-L17
43+
* force running in band, which sets maxWorkers to 1 here:
44+
* https://github.com/facebook/jest/blob/d106643a8ee0ffa9c2f11c6bb2d12094e99135aa/packages/jest-config/src/getMaxWorkers.ts#L27-L28
3945
*/
40-
const isWatchMode = watch || watchAll;
4146
const areFastTests = timings.every(timing => timing < SLOW_TEST_TIME);
4247
const oneWorkerOrLess = maxWorkers <= 1;
4348
const oneTestOrLess = tests.length <= 1;
4449

45-
if (isWatchMode) {
46-
return oneWorkerOrLess || (oneTestOrLess && areFastTests);
47-
}
48-
4950
return (
5051
// When specifying a memory limit, workers should be used
5152
!workerIdleMemoryLimit &&

0 commit comments

Comments
 (0)