Skip to content

Commit

Permalink
fix: bigInt serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravhiremath committed Jun 30, 2021
1 parent edf2803 commit 4f7cd03
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/jest-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-worker/src/workers/messageParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (() => {
Expand All @@ -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');
}
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-worker/src/workers/processChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4f7cd03

Please sign in to comment.