Skip to content

Commit 4beda2d

Browse files
authored
fix: do not report unref-ed subprocesses as open handles (#12705)
1 parent 54f0ded commit 4beda2d

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
- `[jest-config]` [**BREAKING**] Add `mjs` and `cjs` to default `moduleFileExtensions` config ([#12578](https://github.com/facebook/jest/pull/12578))
7676
- `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084))
7777
- `[jest-core]` [**BREAKING**] Exit with status `1` if no tests are found with `--findRelatedTests` flag ([#12487](https://github.com/facebook/jest/pull/12487))
78+
- `[jest-core]` Do not report unref-ed subprocesses as open handles ([#12705](https://github.com/facebook/jest/pull/12705))
7879
- `[jest-each]` `%#` is not replaced with index of the test case ([#12517](https://github.com/facebook/jest/pull/12517))
7980
- `[jest-each]` Fixes error message with incorrect count of missing arguments ([#12464](https://github.com/facebook/jest/pull/12464))
8081
- `[jest-environment-jsdom]` Make `jsdom` accessible to extending environments again ([#12232](https://github.com/facebook/jest/pull/12232))

e2e/__tests__/detectOpenHandles.ts

+11
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ it('does not report timeouts using unref', () => {
108108
expect(textAfterTest).toBe('');
109109
});
110110

111+
it('does not report child_process using unref', () => {
112+
// The test here is basically that it exits cleanly without reporting anything (does not need `until`)
113+
const {stderr} = runJest('detect-open-handles', [
114+
'child_process',
115+
'--detectOpenHandles',
116+
]);
117+
const textAfterTest = getTextAfterTest(stderr);
118+
119+
expect(textAfterTest).toBe('');
120+
});
121+
111122
it('prints out info about open handlers from inside tests', async () => {
112123
const run = runContinuous('detect-open-handles', [
113124
'inside',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const {spawn} = require('child_process');
9+
10+
test('something', () => {
11+
const subprocess = spawn(
12+
process.argv[0],
13+
[require.resolve('../interval-code')],
14+
{
15+
detached: true,
16+
stdio: 'ignore',
17+
},
18+
);
19+
subprocess.unref();
20+
expect(true).toBe(true);
21+
});
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
setInterval(() => {
9+
console.log('XX');
10+
}, 1000);

packages/jest-core/src/collectHandles.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,19 @@ export default function collectHandles(): HandleCollectionResult {
9393
if (fromUser) {
9494
let isActive: () => boolean;
9595

96-
if (type === 'Timeout' || type === 'Immediate') {
97-
// Timer that supports hasRef (Node v11+)
98-
if ('hasRef' in resource) {
99-
if (hasWeakRef) {
100-
// @ts-expect-error: doesn't exist in v12 typings
101-
const ref = new WeakRef(resource);
102-
isActive = () => {
103-
return ref.deref()?.hasRef() ?? false;
104-
};
105-
} else {
106-
isActive = resource.hasRef.bind(resource);
107-
}
96+
// Handle that supports hasRef
97+
if ('hasRef' in resource) {
98+
if (hasWeakRef) {
99+
// @ts-expect-error: doesn't exist in v12 typings
100+
const ref = new WeakRef(resource);
101+
isActive = () => {
102+
return ref.deref()?.hasRef() ?? false;
103+
};
108104
} else {
109-
// Timer that doesn't support hasRef
110-
isActive = alwaysActive;
105+
isActive = resource.hasRef.bind(resource);
111106
}
112107
} else {
113-
// Any other async resource
108+
// Handle that doesn't support hasRef
114109
isActive = alwaysActive;
115110
}
116111

0 commit comments

Comments
 (0)