Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-worker]: bigInt serialization in jest-worker #11624

Closed
wants to merge 6 commits into from
Closed
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 @@ -9,6 +9,7 @@
- `[jest-runtime]` Fix regression when using `jest.isolateModules` and mocks ([#11882](https://github.com/facebook/jest/pull/11882))
- `[jest-runtime]` Include test name when importing modules after test has completed ([#11885](https://github.com/facebook/jest/pull/11885))
- `[jest-runtime]` Error when ESM import is used after test is torn down ([#11885](https://github.com/facebook/jest/pull/11885))
- `[jest-worker]` Add support for serializing `bigInt` objects ([#11624](https://github.com/facebook/jest/pull/11624))

### Chore & Maintenance

Expand Down
2 changes: 2 additions & 0 deletions packages/jest-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
},
"dependencies": {
"@types/node": "*",
"json-bigint": "^1.0.0",
"merge-stream": "^2.0.0",
"supports-color": "^8.0.0"
},
"devDependencies": {
"@types/json-bigint": "^1.0.1",
"@types/merge-stream": "^1.1.2",
"@types/supports-color": "^8.1.0",
"get-stream": "^6.0.0",
Expand Down
21 changes: 19 additions & 2 deletions packages/jest-worker/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let Queue;
beforeEach(() => {
jest.mock('../Farm', () => {
const fakeClass = jest.fn(() => ({
doWork: jest.fn().mockResolvedValue(42),
doWork: jest.fn().mockResolvedValue({bigIntValue: 2n, value: 42}),
}));

return {
Expand Down Expand Up @@ -158,7 +158,24 @@ it('calls doWork', async () => {

const promise = farm.foo('car', 'plane');

expect(await promise).toEqual(42);
expect(await promise).toEqual({bigIntValue: 2n, value: 42});
});

it('checks if bigInt type is preserved', async () => {
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 1,
});

const promise = farm.foo('car', 'plane');

expect(await promise).toEqual(
expect.objectContaining({
/* global BigInt */
bigIntValue: expect.any(BigInt),
value: expect.any(Number),
}),
);
});

it('calls getStderr and getStdout from worker', async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/* eslint-disable local/ban-types-eventually */

import {cpus} from 'os';
import * as JSONbig from 'json-bigint';
import Farm from './Farm';
import WorkerPool from './WorkerPool';
import type {
Expand Down Expand Up @@ -45,6 +46,10 @@ function getExposedMethods(
return exposedMethods;
}

export function serializerWithBigInt(data: unknown): unknown {
Copy link
Member

Choose a reason for hiding this comment

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

this should not be in index to avoid circular dependencies. Please stick it into a utils, serializers or something

return JSONbig.parse(JSONbig.stringify(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 {serializerWithBigInt} 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, serializerWithBigInt(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 {serializerWithBigInt} from '..';
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, serializerWithBigInt(result)]);
}

function reportClientError(error: Error) {
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4648,6 +4648,13 @@ __metadata:
languageName: node
linkType: hard

"@types/json-bigint@npm:^1.0.1":
version: 1.0.1
resolution: "@types/json-bigint@npm:1.0.1"
checksum: 374924d3a3c8620a282c8145751ef60d9daa7b627a0991e88f6a955f7d477853d2fdb9781ac125fa0bf8d2a0cc5edc1e063dcff5619332b9eb94661e0e7196f2
languageName: node
linkType: hard

"@types/json-schema@npm:*, @types/json-schema@npm:^7.0.5, @types/json-schema@npm:^7.0.7, @types/json-schema@npm:^7.0.8":
version: 7.0.9
resolution: "@types/json-schema@npm:7.0.9"
Expand Down Expand Up @@ -6414,6 +6421,13 @@ __metadata:
languageName: node
linkType: hard

"bignumber.js@npm:^9.0.0":
version: 9.0.1
resolution: "bignumber.js@npm:9.0.1"
checksum: 605e9639c413f344c37b23e919254f60a5017cc5ccd925e2f8fb79b36aa3d54f356df9c726f38465263236455f685d60dcf38dbe32cb0b7e4d2a32c94b035476
languageName: node
linkType: hard

"binary-extensions@npm:^1.0.0":
version: 1.13.1
resolution: "binary-extensions@npm:1.13.1"
Expand Down Expand Up @@ -13313,11 +13327,13 @@ fsevents@^1.2.7:
version: 0.0.0-use.local
resolution: "jest-worker@workspace:packages/jest-worker"
dependencies:
"@types/json-bigint": ^1.0.1
"@types/merge-stream": ^1.1.2
"@types/node": "*"
"@types/supports-color": ^8.1.0
get-stream: ^6.0.0
jest-leak-detector: ^27.2.0
json-bigint: ^1.0.0
merge-stream: ^2.0.0
supports-color: ^8.0.0
worker-farm: ^1.6.0
Expand Down Expand Up @@ -13535,6 +13551,15 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"json-bigint@npm:^1.0.0":
version: 1.0.0
resolution: "json-bigint@npm:1.0.0"
dependencies:
bignumber.js: ^9.0.0
checksum: f6c550e09f25416d07d888db88e3cec6c13443b4c0ed5a903dc39b9870fc5adc77aecc30a2d4373888abce7526410a42b0bcf926382a399a285686163a479801
languageName: node
linkType: hard

"json-buffer@npm:3.0.0":
version: 3.0.0
resolution: "json-buffer@npm:3.0.0"
Expand Down