Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
- `[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))
- `[jest-worker]` [**BREAKING**] Use named exports ([#10623] (https://github.com/facebook/jest/pull/10623))
- `[jest-worker]` Do not swallow errors during serialization ([#10984] (https://github.com/facebook/jest/pull/10984))
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))

### Chore & Maintenance
Expand Down
40 changes: 21 additions & 19 deletions packages/jest-worker/src/workers/messageParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@

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

const isWorkerThread = () => {
const isWorkerThread: boolean = (() => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only run this once instead of on each invocation of the messageParent function

try {
// `Require` here to support Node v10
const {isMainThread, parentPort} = require('worker_threads');
return !isMainThread && parentPort;
const {
isMainThread,
parentPort,
} = require('worker_threads') as typeof import('worker_threads');
return !isMainThread && parentPort != null;
} catch {
return false;
}
};
})();

const messageParent = (
export default function messageParent(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should ignore whitespace here - diff is removed try-catch and added type information

https://github.com/facebook/jest/pull/10984/files?w=1

message: unknown,
parentProcess: NodeJS.Process = process,
): void => {
try {
if (isWorkerThread()) {
// `Require` here to support Node v10
const {parentPort} = require('worker_threads');
parentPort.postMessage([PARENT_MESSAGE_CUSTOM, message]);
} else if (typeof parentProcess.send === 'function') {
parentProcess.send([PARENT_MESSAGE_CUSTOM, message]);
}
} catch {
parentProcess = process,
): void {
if (isWorkerThread) {
// `Require` here to support Node v10
const {
parentPort,
} = require('worker_threads') as typeof import('worker_threads');
// ! is safe due to `null` check in `isWorkerThread`
parentPort!.postMessage([PARENT_MESSAGE_CUSTOM, message]);
} else if (typeof parentProcess.send === 'function') {
parentProcess.send([PARENT_MESSAGE_CUSTOM, message]);
} else {
throw new Error('"messageParent" can only be used inside a worker');
}
};

export default messageParent;
}