Skip to content

Have render call cleanup #428

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

Closed
Gpx opened this issue Aug 8, 2019 · 18 comments · Fixed by #430
Closed

Have render call cleanup #428

Gpx opened this issue Aug 8, 2019 · 18 comments · Fixed by #430
Labels
BREAKING CHANGE This change will require a major version bump enhancement New feature or request help wanted Extra attention is needed needs investigation released

Comments

@Gpx
Copy link
Member

Gpx commented Aug 8, 2019

Describe the feature you'd like:

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.

Suggested implementation:

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.

Describe alternatives you've considered:

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.

Teachability, Documentation, Adoption, Migration Strategy:

We would have to remove the documentation about cleanup. Potentially we would have to introduce a breaking change.

@threepointone
Copy link
Contributor

If render called cleanup automatically, what would you make assertions on? :) The application would unmount and the dom element would be removed.

@kentcdodds
Copy link
Member

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.

@Gpx
Copy link
Member Author

Gpx commented Aug 8, 2019

Yes, I meant calling cleanup before doing any rendering

@weyert
Copy link
Contributor

weyert commented Aug 8, 2019

I was wondering how this would affect the rerendering of components?

@kentcdodds
Copy link
Member

I guess we'd need to make sure that calling rerender would not call cleanup.

@alexkrolick
Copy link
Collaborator

alexkrolick commented Aug 8, 2019

🤔 I like the idea of reducing the amount of setup required...

  • It would have to check the container and baseElement identities before cleaning up
  • rerender would have to not do this

This 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

@threepointone
Copy link
Contributor

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?)

@kentcdodds
Copy link
Member

(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.

@alexkrolick
Copy link
Collaborator

non-jest environments

👆

@kentcdodds
Copy link
Member

The more I think about it, the more I think this makes sense. I'm totally open to seeing a PR for this.

@kentcdodds kentcdodds added enhancement New feature or request help wanted Extra attention is needed needs investigation BREAKING CHANGE This change will require a major version bump labels Aug 8, 2019
@alexkrolick
Copy link
Collaborator

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.

@kentcdodds
Copy link
Member

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).

@alexkrolick
Copy link
Collaborator

👍 misread what "this" meant in "this makes sense"

@alexkrolick
Copy link
Collaborator

alexkrolick commented Aug 8, 2019

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.

@kentcdodds
Copy link
Member

Yes, definitely would work with that

kentcdodds pushed a commit that referenced this issue Aug 8, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
@kentcdodds
Copy link
Member

Alright, I feel really great about this. Made #430.

kentcdodds pushed a commit that referenced this issue Aug 8, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
@kentcdodds
Copy link
Member

I'm thinking this'll happen soon. Will require a major version bump though 😬

kentcdodds pushed a commit that referenced this issue Aug 8, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428

BREAKING CHANGE: If your tests were not isolated before (and you referenced the same component between tests) then this change will break your tests. You should [keep your tests isolated](https://kentcdodds.com/blog/test-isolation-with-react), but if you're unable/unwilling to do that right away, then you can either run your tests with the environment variable `RTL_SKIP_AUTO_CLEANUP` set to `true` or import `@testing-library/react/pure` instead of `@testing-library/react`.
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428
kentcdodds pushed a commit that referenced this issue Aug 9, 2019
You can disable this with the RTL_SKIP_CLEANUP environment variable if
you so choose, but it's recommended to have cleanup work this way.

Closes #428

BREAKING CHANGE: If your tests were not isolated before (and you referenced the same component between tests) then this change will break your tests. You should [keep your tests isolated](https://kentcdodds.com/blog/test-isolation-with-react), but if you're unable/unwilling to do that right away, then you can either run your tests with the environment variable `RTL_SKIP_AUTO_CLEANUP` set to `true` or import `@testing-library/react/pure` instead of `@testing-library/react`.
@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 9.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE This change will require a major version bump enhancement New feature or request help wanted Extra attention is needed needs investigation released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants