-
-
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
Problems with Mocha / Jsdom / React testing #1722
Comments
I fear I'm out of my depth here.. suspecting it's an issue with jsdom though. |
It looks like React is using memoization of a DOM node when it is first instantiated, which is also the reason for the need to have created a jsdom document and leaking a global It seems that the leak across tests is caused by the require cache being shared across test instances, resulting in a React instance in test-2 which uses a dom node from a different document to create it's document elements. This is what causes the Would there be a way to configure mocha not to use requires cache between tests, or even have it run each test in separated processes? |
According to this quite old issue (#536), the only way to make mocha use a new process/context for each test suite is to run test files individually... Which is of course a non solution. Thus the need of a more canonical way of doing it (disable caching entirely could also be an option, if possible) |
Ran into a similar problem a while ago, my solution was to "undefine" requirejs modules after every test. I wrapped requirejs.require in a function that pushes the name of the module it's requiring to an array. Added a function called reset() that goes through and calls undef on each of the modules in the array. I call reset() after every test suite. |
@akhaku perhaps you'd be interested in https://github.com/sindresorhus/require-uncached |
I'm afraid I'm going to have to close the issue, as it's not a problem with mocha itself but the specific usage of it in this case which leaks context between tests.
The first would be possible, using something like |
I see, that's too bad. @akhaku I'm doing something similar in Maybe something more clever will come to mind one day ;) Thanks guys |
@jbnicolai Thanks for taking the time to look into this |
It's really an issue with nodejs/require, but I agree that it's an issue and that it'd be nice if mocha would work around it somehow. Thing is, un-caching everthing is really, really, slow.. :( For reference: this issue is a duplicate/has the same underlying cause as #736 |
This is a crosspost with facebook/react#4025 because I'm not sure if the problem is with Mocha or React
I'm not entirely sure if what I'm having here is a problem with Mocha or React, but I really need some help figuring out why my tests are failing with what looks like leaks across siloed documents.
My test setup is using Mocha, where each individual test spins up a document with jsdom before loading React. I then proceed to run the test of the individual component in each of these separate documents.
My tests run fine if I only run a single file at a time, but when I run all tests I get errors like
DOMException: Wrong document
(from jsdom) andError: Invariant Violation: findComponentRoot(..., .1): Unable to find element...
(from React).I've created a repository with a minimal setup and a more detailed description here: https://github.com/Munter/MochaJsdomReact
Can anyone help me figure out what the problem is, and how to avoid it, or maybe even fix it?
The text was updated successfully, but these errors were encountered: