Skip to content

Commit 0c75d62

Browse files
authored
fix(worker): do not swallow errors when sending custom messages (#10984)
1 parent 0ab070c commit 0c75d62

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
4949
- `[jest-transform]` [**BREAKING**] Refactor API of transformers to pass an options bag rather than separate `config` and other options ([#10834](https://github.com/facebook/jest/pull/10834))
5050
- `[jest-worker]` [**BREAKING**] Use named exports ([#10623] (https://github.com/facebook/jest/pull/10623))
51+
- `[jest-worker]` Do not swallow errors during serialization ([#10984] (https://github.com/facebook/jest/pull/10984))
5152
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))
5253

5354
### Chore & Maintenance

packages/jest-worker/src/workers/messageParent.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,33 @@
77

88
import {PARENT_MESSAGE_CUSTOM} from '../types';
99

10-
const isWorkerThread = () => {
10+
const isWorkerThread: boolean = (() => {
1111
try {
1212
// `Require` here to support Node v10
13-
const {isMainThread, parentPort} = require('worker_threads');
14-
return !isMainThread && parentPort;
13+
const {
14+
isMainThread,
15+
parentPort,
16+
} = require('worker_threads') as typeof import('worker_threads');
17+
return !isMainThread && parentPort != null;
1518
} catch {
1619
return false;
1720
}
18-
};
21+
})();
1922

20-
const messageParent = (
23+
export default function messageParent(
2124
message: unknown,
22-
parentProcess: NodeJS.Process = process,
23-
): void => {
24-
try {
25-
if (isWorkerThread()) {
26-
// `Require` here to support Node v10
27-
const {parentPort} = require('worker_threads');
28-
parentPort.postMessage([PARENT_MESSAGE_CUSTOM, message]);
29-
} else if (typeof parentProcess.send === 'function') {
30-
parentProcess.send([PARENT_MESSAGE_CUSTOM, message]);
31-
}
32-
} catch {
25+
parentProcess = process,
26+
): void {
27+
if (isWorkerThread) {
28+
// `Require` here to support Node v10
29+
const {
30+
parentPort,
31+
} = require('worker_threads') as typeof import('worker_threads');
32+
// ! is safe due to `null` check in `isWorkerThread`
33+
parentPort!.postMessage([PARENT_MESSAGE_CUSTOM, message]);
34+
} else if (typeof parentProcess.send === 'function') {
35+
parentProcess.send([PARENT_MESSAGE_CUSTOM, message]);
36+
} else {
3337
throw new Error('"messageParent" can only be used inside a worker');
3438
}
35-
};
36-
37-
export default messageParent;
39+
}

0 commit comments

Comments
 (0)