-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
TypeError: cyclic object value with JSON.stringify #4910
Comments
I've experienced this issue as well in https://github.com/mui/mui-x (mui/mui-x#5851 (comment) specifically) const element = document.createElement('div');
element.reference = element;
stringify(element); // will throw But I wonder if that can be avoided on Removing So I see two options here:
|
I would vote for 1. The logic was introduced in karma-runner/karma-mocha#85 for being able to tap into internal APIs, but it seems that not even the author of the PR needs karma-mocha to stringify the expected and actual keys, he could do this on his own. |
Hmm, HTML elements aren't going to be the only cyclic object values that break Mocha in this way. Synthetic React elements per the OP are an immediate other example. I think @jmlopez-rod's solution of manually adding in cycle detection for Copying code from the
|
@JoshuaKGoldberg I would go with an established library like |
Looks like this has since been fixed! I updated https://github.com/jmlopez-rod/mocha-stringify-issue to use My hunch is this is a duplicate of #5032. If anybody is still seeing cyclic object crashes, please do file a new issue with an isolated reproduction. Cheers! 🤎 |
Prerequisites
faq
labelnode_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
Mocha uses
JSON.stringify
without a replacer when dealing with objects that have circular references in the browser.Steps to Reproduce
See https://github.com/jmlopez-rod/mocha-stringify-issue
Seems that react elements have cyclic references. I was able to fix the issue by overriding the definition of
JSON.stringify
sothat it uses a replacer to deal with cyclic references following the example in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value
Expected behavior: mocha should not be stuck trying to stringify react elements.
The above screenshot includes a simple quick fix to make it fail fast.
Actual behavior: In the browser we can see where the error is coming from
The error is
Due to this error we can see that karma disconnects.
Reproduces how often: Every run I've made in a mac and linux I see the issue.
Versions
mocha --version
andnode_modules/.bin/mocha --version
:node --version
:Additional Information
See the files in the repo with the reproduction of the issue
https://github.com/jmlopez-rod/mocha-stringify-issue
For now the workaround I use is to monkeypatch
JSON.stringify
when running tests.The text was updated successfully, but these errors were encountered: