diff --git a/CHANGELOG.md b/CHANGELOG.md index e78bd56e2053..cbe103308c3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-worker/src/workers/messageParent.ts b/packages/jest-worker/src/workers/messageParent.ts index bf0f828cbb63..0e65c835a6ab 100644 --- a/packages/jest-worker/src/workers/messageParent.ts +++ b/packages/jest-worker/src/workers/messageParent.ts @@ -7,31 +7,33 @@ import {PARENT_MESSAGE_CUSTOM} from '../types'; -const isWorkerThread = () => { +const isWorkerThread: boolean = (() => { 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( 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; +}