Skip to content

await act(...) inside cleanup-after-each #427

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

Merged
merged 1 commit into from
Aug 6, 2019

Conversation

threepointone
Copy link
Contributor

@threepointone threepointone commented Aug 6, 2019

What:

This PR adds an async act() call to cleanup-after-each.

Why:

Tests, especially those with async logic, could have hanging microtasks or React work that could leak into the next test.

How:

This PR

  • adds cleanup-async.js (so I can write tests for it)
  • awaits an act(async () => {}) call before unmounting containers
  • And a test for the same

Some possible Q&A:

  • Why not do a sync act() in cleanup()?: If peeps are using react-testing-library, it's suuuper unlikely they'll have hanging sync effects/updates. Decided not to add code without a good reason.

(bonus: fixes a lint violation in act-compat.js)

Checklist:

  • Documentation added to the docs site - NA
  • Tests
  • Typescript definitions updated - NA
  • Ready to be merged

@threepointone
Copy link
Contributor Author

threepointone commented Aug 6, 2019

I wonder how this would work with resetModules(). I might update the PR to be a bit more defensive.

@alexkrolick
Copy link
Collaborator

Why not make cleanup() return a promise?: That would be a breaking change, especially for people already using cleanup() in their tests. You could consider it for a next major.

I think we would remove cleanupAsync if we changed cleanup to return a promise, so that would create double thrash for anyone who switches now.

What if we do the breaking change release now and either document a workaround for the current version or release this as a non-recommended minor version on top of the current release?

@threepointone
Copy link
Contributor Author

I think we would remove cleanupAsync if we changed cleanup to return a promise, so that would create double thrash for anyone who switches now.

Right, so don't even consider cleanupAsync as a public api right now?

@threepointone
Copy link
Contributor Author

I updated the PR to do a dynamic require() in cleanup-after-each

@threepointone
Copy link
Contributor Author

I could even just move the code into cleanup-after-each, and not have a new export at all

@alexkrolick
Copy link
Collaborator

alexkrolick commented Aug 6, 2019

Right, so don't even consider cleanupAsync as a public api right now?

I suspect these usages are just as common if not more so than import "@testing-library/react/cleanup-after-each" :

afterEach(cleanup)
afterEach(() => {
  // Something else ...
  cleanup()
})

It's not a breaking change for the first one to return a promise though


@kentcdodds maybe ask via the TestingLib twitter what people are using?

@kentcdodds
Copy link
Member

Yeah, I wish that people would just import the thing, but I suspect many are using cleanup directly 😔 I'll do a twitter poll for curiosity reasons, but I think we should leave this PR as-is, not document cleanupAsync and then include it in the next major. I don't see much reason to make a breaking change for this feature.

@threepointone
Copy link
Contributor Author

Alright, I updated the PR so it's only in cleanup-after-each now, and we can decouple this PR from whatever the call on cleanup is

@kentcdodds
Copy link
Member

Here it is: https://twitter.com/TestingLib/status/1158795165420572672

kentcdodds
kentcdodds previously approved these changes Aug 6, 2019
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. What do you think @alexkrolick?

- awaits an `act(async () => {})` inside `cleanup-after-each`

Some possible Q&A:
- why not do the same in sync cleanup()?: if peeps are using react-testing-library already, it's suuuper unlikely they'll have hanging sync effects/updates. Decided not to add code without a good reason.

(bonus: fixes a lint violation in act-compat.js)
Copy link
Collaborator

@alexkrolick alexkrolick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎸

alexkrolick
alexkrolick approved these changes Aug 6, 2019
@kentcdodds kentcdodds merged commit 1eae361 into testing-library:master Aug 6, 2019
@kentcdodds
Copy link
Member

Thanks Sunil!

@threepointone threepointone deleted the flush-on-cleanup branch August 6, 2019 20:14
@kentcdodds
Copy link
Member

🎉 This PR is included in version 8.0.8 🎉

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants