From 4f7cd0317545efdd0fb652841f2460753d485461 Mon Sep 17 00:00:00 2001 From: Saurav M H Date: Wed, 30 Jun 2021 17:20:53 +0000 Subject: [PATCH] fix: bigInt serialization --- packages/jest-worker/src/index.ts | 7 +++++++ packages/jest-worker/src/workers/messageParent.ts | 3 ++- packages/jest-worker/src/workers/processChild.ts | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/jest-worker/src/index.ts b/packages/jest-worker/src/index.ts index 50be5683dfba..a563d79781e3 100644 --- a/packages/jest-worker/src/index.ts +++ b/packages/jest-worker/src/index.ts @@ -45,6 +45,13 @@ function getExposedMethods( return exposedMethods; } +export function serializeBigInt(data: unknown): unknown { + if (typeof data === 'bigint') { + return data.toString(); + } + return data; +} + /** * The Jest farm (publicly called "Worker") is a class that allows you to queue * methods across multiple child processes, in order to parallelize work. This diff --git a/packages/jest-worker/src/workers/messageParent.ts b/packages/jest-worker/src/workers/messageParent.ts index d4a389055fc0..662e51db1643 100644 --- a/packages/jest-worker/src/workers/messageParent.ts +++ b/packages/jest-worker/src/workers/messageParent.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import {serializeBigInt} from '../index'; import {PARENT_MESSAGE_CUSTOM} from '../types'; const isWorkerThread: boolean = (() => { @@ -29,7 +30,7 @@ export default function messageParent( // ! 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]); + parentProcess.send([PARENT_MESSAGE_CUSTOM, serializeBigInt(message)]); } else { throw new Error('"messageParent" can only be used inside a worker'); } diff --git a/packages/jest-worker/src/workers/processChild.ts b/packages/jest-worker/src/workers/processChild.ts index 64d29e19e132..5359b3f5f765 100644 --- a/packages/jest-worker/src/workers/processChild.ts +++ b/packages/jest-worker/src/workers/processChild.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import {serializeBigInt} from '../index'; import { CHILD_MESSAGE_CALL, CHILD_MESSAGE_END, @@ -64,7 +65,7 @@ function reportSuccess(result: unknown) { throw new Error('Child can only be used on a forked process'); } - process.send([PARENT_MESSAGE_OK, result]); + process.send([PARENT_MESSAGE_OK, serializeBigInt(result)]); } function reportClientError(error: Error) {