question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jest-worker fails with "Do not know how to serialize a BigInt" in `messageParent.ts` instead of showing the actual assertion error message when there are 2 or more test suite files executing in parallel

See original GitHub issue

Link to repl or repo (highly encouraged)

Minimal reproduction repo: jest-bigint-worker-issue

🐛 Bug Report

When you have multiple test suite files executing in parallel, and some of the tests are asserting values with bigint numbers in them and the assertion fails, instead of showing this assertion error message, jest-worker fails itself on attempt to serialise this value during the call to the:

parentProcess.send([_types().PARENT_MESSAGE_CUSTOM, message]);

(in messageParent.js)

I assume that’s because message includes failureDetails.matcherResult holding the compared bigint values that can’t be passed to process.send(), as unlike worker_threads it does not support non-json values out of the box.

To Reproduce

Steps to reproduce the behavior:

git clone git@github.com:klesun-productions/jest-bigint-worker-issue.git # clone minimal reproduction repo
cd jest-bigint-worker-issue
npm ci # install jest dependency
npm test # run the test that reproduces the issue

Expected behavior

You should have seen the assertion error informing you that expect(1n).toEqual(2n); expectation failed

Actual behavior

But instead you get following output due to an internal jest-worker error:

 PASS  tests/some-other.test.js
  ✓ should succeed (2 ms)

 FAIL  tests/bigint.test.js
  ● Test suite failed to run

    TypeError: Do not know how to serialize a BigInt
        at stringify (<anonymous>)

      at messageParent (node_modules/jest-worker/build/workers/messageParent.js:42:19)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.777 s

With no details of what the actual error that jest-worker tried to report was in tests/bigint.test.js.

envinfo

  System:
    OS: macOS 11.3.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.17.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.13 - /usr/local/bin/npm
  npmPackages:
    jest: ^27.0.5 => 27.0.5

Misc

I assume that’s a transport issue that user of jest is not supposed to be aware of. One thing I did that at least somehow solved the absence of the real message was patching the jest-worker wrapping the call to parentProcess.send() in a try-catch and console.log-ing the message whose serialisation failed:

try {
  parentProcess.send([_types().PARENT_MESSAGE_CUSTOM, message]);
} catch (error) {
  console.error('jest-worker message serialisation failed', error);
  console.dir(message, {depth: 10});
  throw error;
}

There is likely a number of ways how to better address this problem…

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:27
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
kaptainkommiecommented, Aug 4, 2022

Any update?

7reactions
gomaincommented, Feb 3, 2022

For those of you that got here because a failing test involves some BigInt, you can temporarily circumvent this issue by setting maxWorkers: 1 in your jest configuration.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest messageParent can only be used inside a worker
If all of this is correct, then Jest offers two ways to test worker code, and you're trying to mix the two techniques,...
Read more >
Property 'toBeInTheDocument' does not exist on type 'Assertion'.
Existing Next.js Typescript Project and trying to implement Jest. TS Error: Property 'toBeInTheDocument' does not exist on type 'Assertion'.
Read more >
JUnit 5 Lazy Assertion Message Tutorial - Apps Developer Blog
This tutorial teaches how to use a Lazy Assertion message in JUnit 5. ... more information about the failed test and help in...
Read more >
Asserting Expectations - The Debugging Book
Assertions show their true power when they are not used in a test, but used in a program instead, because that is when...
Read more >
java - Purpose of assertion messages
You seem to be dividing the people that may encounter the errors thrown from the assertion into two categories: You, which should be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found