Right now we have to call cleanup before each test. This tends to be confusing for beginners especially if they come from other libraries (Enzyme) that do not need this step. It would be good if render could call cleanup automatically.
I didn't dig into the code too much but it should be as easy as having render call cleanup as the first thing it does.
I'm trying to think about a use-case where you might want to run two renders in the same test or where you might want to manually run cleanup—which would invalidate this idea—but can't come up with anything.
We would have to remove the documentation about cleanup. Potentially we would have to introduce a breaking change.
If render called cleanup automatically, what would you make assertions on? :) The application would unmount and the dom element would be removed.
I think the suggestion is to call cleanup _before_ rendering. I'm considering this... I'm leaning toward "no" because it violates the principle of least surprise. But I want to hear what other people have to say.
Yes, I meant calling cleanup before doing any rendering
I was wondering how this would affect the rerendering of components?
I guess we'd need to make sure that calling rerender would not call cleanup.
🤔 I like the idea of reducing the amount of setup required...
container and baseElement identities before cleaning uprerender would have to not do thisThis might make sense at a higher level of abstraction like the const page = visit('/route') thing @mxstbr demoed, where render is basically equivalent to a page refresh
Let’s say a component has a bug where unmounting throws an error (the effect cleanup is bad, eg). Then this feature would make the next test fail, and debugging would be a pain.
Not sure if this is worth the short term gain, tbh. The ‘fix’ should be figuring out how to automate cleanup.
(As an example, why not “import ‘cleanup-after-each’” by default on importing rtl?)
(As an example, why not “import ‘cleanup-after-each’” by default on importing rtl?)
I've considered that. If we made cleanup-after-each work in non-jest environments then I'd be 👍 for that I think.
non-jest environments
👆
The more I think about it, the more I think this makes sense. I'm totally open to seeing a PR for this.
Let’s say a component has a bug where unmounting throws an error (the effect cleanup is bad, eg). Then this feature would make the next test fail, and debugging would be a pain.
This seems like a big deal though.
It is a big deal. What Sunil suggested and what I want is for import 'cleanup-after-each' to happen automatically when you import @testing-library/react (and to do some typeof afterEach checks so it doesn't break in other environments).
👍 misread what "this" meant in "this makes sense"
import 'cleanup-after-each' to happen automatically when you import @testing-library/react (and to do some typeof afterEach checks so it doesn't break in other environments)
One additional thing to confirm is that this works if you have a custom test utils file that imports and re-exports from RTL.
Yes, definitely would work with that
Alright, I feel really great about this. Made #430.
I'm thinking this'll happen soon. Will require a major version bump though 😬
:tada: This issue has been resolved in version 9.0.0 :tada:
The release is available on:
npm package (@latest dist-tag)Your semantic-release bot :package::rocket: