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
Circular references hang jest when assertions fail on node 14 #10577
Comments
Is there any workaround? |
@Lonli-Lokli Looks like the only workaround for now is However, this causes a massive decrease in performance. I hope this can be fixed soon. I have migrated several of our projects to jest... and now it causes hiccups all throughout our build system when one test breaks. It would be nice if |
I can reproduce this when I have two such tests (in separate files - but not sure if this matters) and I have to run it with |
I just ran into what I assume is the same issue on Node 10.16.0 and Jest 26.6.2. I can't reproduce it as written, but if I make a file containing two copies of @voces's example test and run it in watch mode, I get the same error. The test: it('test', () => {
const foo = {};
foo.ref = foo;
expect(foo).toEqual({});
});
it('test 2', () => {
const foo = {};
foo.ref = foo;
expect(foo).toEqual({});
}); The error:
Running with |
This is happening since #9496 And it seems to happen because of some inter-process serialization. After adding |
Note that after commit 5f6f2ec which changes default runner to circus, the error message is
with |
Added a PR as an attempt to fix this. Also, please note that the |
🎉
We should not swallow errors like that... |
We confirm it happens in Node 12, and it’s more common when using Angular Dependency Injection (I think they have cyclic structures in some error-states). The process hangs in such scenario, but this can be improved slightly be applying |
I can confirm this problem as very common with Angular DI with Node 12 as @piotrl mentioned. How can I run it with //package.json > scripts
"test:unit": "node --unhandled-rejections=strict $(npm bin)/jest --env=jest-environment-jsdom-sixteen ", this is actually stopping the execution, is that the right workaround? |
…0577 as we use circular references
I get this issue with mocking await functions in many tests in different
If I do |
@KrisBNelson yeah, the circular reference error does not happen with |
@gaurav5430 because with It's only when Jest has N child worker processes that, when a test fails in the child worker, the assertion failure needs to be serialized (via JSON) and sent to the parent process, and then a "cannot convert to JSON" error happens. |
what's the actual solution for this issue? would downgrading jest work ? |
Any solution for this issue? |
@SimenB Kindly have this bug fixed, please! |
hey, any solution on this issue ? |
Similar problem - |
Node v16.19 Jest 29.5 simply calling a method that raises an error with a circular reference. Try/catching the method call and replacing the error in the catch is a work-around. Without that, jest throws the unintuitive error message. Example:
The underlying dependency of the package throwing the error in this case is Axios. The root problem with Jest is assuming JSON.stringify is always going to work. Use a safe stringify implementation, please. |
Currently a solution could be Note that it is marked experimental. That is recently added feature and it is not tested widely yet. |
i was getting the same error for ZoneJs object with circular structure
now getting the real error with stacktrace message and without any "Converting circular structure to JSON" errors A more correct approach might be to set null for each property of failureDetails item except 'stack' and 'message' props jest v28.1.0 or 29.5.0 |
With jest@29.5.0, the following generic workaround helped me: function deCircle(o, seen = new Set()) {
if (!o || typeof o !== 'object') return;
Object.entries(o).forEach(([k,v]) => {
if (seen.has(v)) {
o[k] = null;
return;
}
const s = new Set(seen);
s.add(v);
deCircle(v, s);
})
}
function reportSuccess(result) {
if (!process || !process.send) {
throw new Error('Child can only be used on a forked process');
}
deCircle(result); // ← patching the object
process.send([_types.PARENT_MESSAGE_OK, result]);
} You might need to patch on error as well, depending on the circumstances. |
I had this problem a few minutes ago. Found out it happened when I had two test files with same name in different folders. Once I renamed one of the files the problem was gone. |
Glad to see this is not a general problem with testing eslint rules. In my case, when a test fails, because almost all eslint nodes have circular references this problem is quite common. |
Still happening even though I am just expecting a number to be there and even thought it is but still tests fails 😭 The Solution that worked for me |
Coming back again to the same issue, it seems that this time adding TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'res' -> object with constructor 'Object'
--- property 'req' closes the circle
at stringify (<anonymous>)
at writeChannelMessage (node:internal/child_process/serialization:159:20)
at process.target._send (node:internal/child_process:852:17)
at process.target.send (node:internal/child_process:752:19)
at reportSuccess (/home/kasir/projects/you-say/node_modules/jest-worker/build/workers/processChild.js:82:11)
Node.js v20.10.0
FAIL backend-e2e apps/backend-e2e/src/auth/auth-business.spec.ts
● Test suite failed to run
Jest worker encountered 4 child process exceptions, exceeding retry limit
at ChildProcessWorker.initialize (../../node_modules/jest-worker/build/workers/ChildProcessWorker.js:181:21) |
Trying to stringify the
Try comparing the properties of the request or response object that you care about rather than the whole object itself. I.e. |
So @sawvox what you're saying is that if I just compare what I wanted in the |
Today I've started seeing similar errors on a repository that I've been working on for 4 years. Funny thing is, it's really random - sometimes it works, sometimes it doesn't. Flags like node:internal/child_process/serialization:159
const string = JSONStringify(message) + '\n';
^
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'socket' -> object with constructor 'Object'
--- property '_httpMessage' closes the circle
at stringify (<anonymous>)
at writeChannelMessage (node:internal/child_process/serialization:159:20)
at process.target._send (node:internal/child_process:838:17)
at process.target.send (node:internal/child_process:738:19)
at reportSuccess (/usr/src/app/node_modules/.pnpm/jest-worker@29.7.0/node_modules/jest-worker/build/workers/processChild.js:82:11)
Node.js v18.16.1 Also tried the suggested DataCloneError: function transformRequest(data, headers) {
const contentType = headers.getContentType() || '';
const ha...<omitted>... } could not be cloned.
at messageParent (../../node_modules/.pnpm/jest-worker@29.7.0/node_modules/jest-worker/build/workers/messageParent.js:24:34) This is on Jest |
🐛 Bug Report
When an assertion fails where either the expected or actual value is circular, and both values are objects, jest encounters an error stating it failed to convert a circular structure to JSON, resulting in the test run not completing.
To Reproduce
Running jest gives me the following error:
Jest continues running indefinitely (I only tested up to ten minutes) and reports nothing regarding the test suite.
I traced this to the added
failureDetails
property on error messages, landed in 26.3.0.Expected behavior
I'd expect the test to fail and jest to complete running.
envinfo
I only tested two versions. The above error occurs on 14.9.0, but does not on 12.16.1.
The text was updated successfully, but these errors were encountered: