Skip to content

Commit f435f01

Browse files
authored
detectOpenHandles imply runInBand (#8283)
* pass config into shouldRunInBand * detectOpenHandles imply runInBand * Update CHANGELOG.md * docs
1 parent 650c0b5 commit f435f01

6 files changed

Lines changed: 30 additions & 27 deletions

File tree

CHANGELOG.md

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

77
- `[jest-snapshot]` Inline snapshots: do not indent empty lines ([#8277](https://github.com/facebook/jest/pull/8277))
8+
- `[jest-core]` Make `detectOpenHandles` imply `runInBand` ([#8283](https://github.com/facebook/jest/pull/8283))
89

910
### Chore & Maintenance
1011

docs/CLI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Print debugging info about your Jest config.
158158

159159
### `--detectOpenHandles`
160160

161-
Attempt to collect and print open handles preventing Jest from exiting cleanly. Use this in cases where you need to use `--forceExit` in order for Jest to exit to potentially track down the reason. Implemented using [`async_hooks`](https://nodejs.org/api/async_hooks.html), so it only works in Node 8 and newer.
161+
Attempt to collect and print open handles preventing Jest from exiting cleanly. Use this in cases where you need to use `--forceExit` in order for Jest to exit to potentially track down the reason. This implies `--runInBand`, making tests run serially. Implemented using [`async_hooks`](https://nodejs.org/api/async_hooks.html), so it only works in Node 8 and newer. This option has a significant performance penalty and should only be used for debugging.
162162

163163
### `--env=<environment>`
164164

packages/jest-cli/src/cli/args.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export const options = {
229229
default: false,
230230
description:
231231
'Print out remaining open handles preventing Jest from exiting at the ' +
232-
'end of a test run.',
232+
'end of a test run. Implies `runInBand`.',
233233
type: 'boolean' as 'boolean',
234234
},
235235
env: {

packages/jest-core/src/TestScheduler.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ export default class TestScheduler {
8686
getEstimatedTime(timings, this._globalConfig.maxWorkers) / 1000,
8787
);
8888

89-
const runInBand = shouldRunInBand(
90-
tests,
91-
this._globalConfig.watch || this._globalConfig.watchAll,
92-
this._globalConfig.maxWorkers,
93-
timings,
94-
);
89+
const runInBand = shouldRunInBand(tests, timings, this._globalConfig);
9590

9691
const onResult = async (test: Test, testResult: TestResult) => {
9792
if (watcher.isInterrupted()) {

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,24 @@ const getTestMock = () => ({
2323
const getTestsMock = () => [getTestMock(), getTestMock()];
2424

2525
test.each`
26-
tests | watch | maxWorkers | timings | expectedResult
27-
${[getTestMock()]} | ${true} | ${undefined} | ${[500, 500]} | ${true}
28-
${getTestsMock()} | ${true} | ${1} | ${[2000, 500]} | ${true}
29-
${getTestsMock()} | ${true} | ${2} | ${[2000, 500]} | ${false}
30-
${[getTestMock()]} | ${true} | ${undefined} | ${[2000, 500]} | ${false}
31-
${getTestMock()} | ${true} | ${undefined} | ${[500, 500]} | ${false}
32-
${getTestsMock()} | ${false} | ${1} | ${[2000, 500]} | ${true}
33-
${getTestMock()} | ${false} | ${2} | ${[2000, 500]} | ${false}
34-
${[getTestMock()]} | ${false} | ${undefined} | ${[2000]} | ${true}
35-
${getTestsMock()} | ${false} | ${undefined} | ${[500, 500]} | ${true}
36-
${new Array(45)} | ${false} | ${undefined} | ${[500]} | ${false}
37-
${getTestsMock()} | ${false} | ${undefined} | ${[2000, 500]} | ${false}
26+
tests | timings | detectOpenHandles | maxWorkers | watch | expectedResult
27+
${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${true}
28+
${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${true}
29+
${getTestsMock()} | ${[2000, 500]} | ${false} | ${2} | ${true} | ${false}
30+
${[getTestMock()]} | ${[2000, 500]} | ${false} | ${undefined} | ${true} | ${false}
31+
${getTestMock()} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${false}
32+
${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${false} | ${true}
33+
${getTestMock()} | ${[2000, 500]} | ${false} | ${2} | ${false} | ${false}
34+
${[getTestMock()]} | ${[2000]} | ${false} | ${undefined} | ${false} | ${true}
35+
${getTestsMock()} | ${[500, 500]} | ${false} | ${undefined} | ${false} | ${true}
36+
${new Array(45)} | ${[500]} | ${false} | ${undefined} | ${false} | ${false}
37+
${getTestsMock()} | ${[2000, 500]} | ${false} | ${undefined} | ${false} | ${false}
38+
${getTestsMock()} | ${[2000, 500]} | ${true} | ${undefined} | ${false} | ${true}
3839
`(
3940
'shouldRunInBand() - should return $expectedResult for runInBand mode',
40-
({tests, watch, maxWorkers, timings, expectedResult}) => {
41-
expect(shouldRunInBand(tests, watch, maxWorkers, timings)).toBe(
42-
expectedResult,
43-
);
41+
({tests, timings, detectOpenHandles, maxWorkers, watch, expectedResult}) => {
42+
expect(
43+
shouldRunInBand(tests, timings, {detectOpenHandles, maxWorkers, watch}),
44+
).toBe(expectedResult);
4445
},
4546
);

packages/jest-core/src/testSchedulerHelper.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {Config} from '@jest/types';
89
import {Test} from 'jest-runner';
910

1011
const SLOW_TEST_TIME = 1000;
1112

1213
export function shouldRunInBand(
1314
tests: Array<Test>,
14-
isWatchMode: boolean,
15-
maxWorkers: number,
1615
timings: Array<number>,
16+
{detectOpenHandles, maxWorkers, watch, watchAll}: Config.GlobalConfig,
1717
) {
18-
/**
18+
// detectOpenHandles makes no sense without runInBand, because it cannot detect leaks in workers
19+
if (detectOpenHandles) {
20+
return true;
21+
}
22+
23+
/*
1924
* Run in band if we only have one test or one worker available, unless we
2025
* are using the watch mode, in which case the TTY has to be responsive and
2126
* we cannot schedule anything in the main thread. Same logic applies to
@@ -26,6 +31,7 @@ export function shouldRunInBand(
2631
* force running in band.
2732
* https://github.com/facebook/jest/blob/700e0dadb85f5dc8ff5dac6c7e98956690049734/packages/jest-config/src/getMaxWorkers.js#L14-L17
2833
*/
34+
const isWatchMode = watch || watchAll;
2935
const areFastTests = timings.every(timing => timing < SLOW_TEST_TIME);
3036
const oneWorkerOrLess = maxWorkers <= 1;
3137
const oneTestOrLess = tests.length <= 1;

0 commit comments

Comments
 (0)