Skip to content
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

Global cleanup #199

Merged
merged 4 commits into from Oct 16, 2019
Merged

Global cleanup #199

merged 4 commits into from Oct 16, 2019

Conversation

mpeyper
Copy link
Member

@mpeyper mpeyper commented Oct 14, 2019

What:

Added cleanup functionality to unmount any rendered test components.

Resolved #76

Why:

Currently, the underlying rendered test component is not necessarily unmounted at the end of a test, resulting in effects from useEffect and useLayoutEffect hanging around between tests.

How:

The library now exports a global cleanup function that will unmount any rendered hooks. As per testing-library/react-testing-library#430 we also auto regester an afterEach handler in supported test frameworks (with an environment variable opt out).

Checklist:

  • Documentation updated
  • Tests
  • Ready to be merged

@codecov
Copy link

codecov bot commented Oct 14, 2019

Codecov Report

Merging #199 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #199   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      2    +1     
  Lines          45     56   +11     
  Branches        4      5    +1     
=====================================
+ Hits           45     56   +11
Impacted Files Coverage Δ
src/cleanup.js 100% <100%> (ø)
src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eb400b1...217e299. Read the comment docs.

@mpeyper mpeyper changed the title Pr/cleanup Global cleanup Oct 14, 2019
}

// Automatically registers cleanup in supported testing frameworks
if (typeof afterEach === 'function' && !process.env.RHTL_SKIP_AUTO_CLEANUP) {
Copy link

@hartzis hartzis Oct 14, 2019

Choose a reason for hiding this comment

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

📓 process.env.RHTL_SKIP_AUTO_CLEANUP only needs to be "truthy" and not true to skip.

Do we want this to explicitly be the string "true"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, this is implemented as per @testing-library/react-testing-library

Copy link

Choose a reason for hiding this comment

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

Cool. Nice to see sharing the pattern from RTL for dev familiarity.

Comment on lines +117 to +119
> Please note that this is done automatically if the testing framework you're using supports the
> `afterEach` global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups
> after each test.
Copy link

Choose a reason for hiding this comment

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

❓ Do you happen to have any reference or example of a testing library auto adding a global afterEach?

I think I like this, i'm just curious about community best practices and such.

Copy link
Member Author

Choose a reason for hiding this comment

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

The registration of this is lifted almost verbatim from @testing-library/react-testing-library#430

I don't think this is a commonly used pattern (yet).

})

test('second', () => {
expect(cleanupCalled).toBe(false)
Copy link

Choose a reason for hiding this comment

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

❓ Is this test supposed to check true, expect(cleanupCalled).toBe(true)?

It appears to be a copy of the above test, autoCleanup.disabled.test.js, but without the process.env setup.

You may need to clear the require cache and reset the process.env before running?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. In the beforeAll am overriding afterEach to not be a function to test the case where the testing framework does not support it. I was very worried about this, but I tested thoroughly and jest brings it back for the next test.

Happy to hear ideas for better ways to test this case.

Copy link

Choose a reason for hiding this comment

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

welp jest is pretty damn cool.

Copy link

@hartzis hartzis left a comment

Choose a reason for hiding this comment

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

Just thought I'd try to help out with some small things I noticed.

Great stuff @mpeyper !

Copy link

@hartzis hartzis left a comment

Choose a reason for hiding this comment

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

Nice! Does this need to be a major bump?

@mpeyper
Copy link
Member Author

mpeyper commented Oct 15, 2019

Yeah, will have to be a major, even though I doubt anyone will actually be affected by the break.

@mpeyper
Copy link
Member Author

mpeyper commented Oct 16, 2019

Ok, I'm going to merge this now and get a major release out.

@hartzis, if you would like to, you are welcome to submit a PR adding yourself as a contributor for your efforts reviewing this PR.

@mpeyper mpeyper merged commit 09c2fdd into master Oct 16, 2019
@mpeyper mpeyper deleted the pr/cleanup branch June 8, 2020 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Global unmountAll functionality
2 participants