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

Run cleanup() when test runner supports teardown() #676

Merged
merged 3 commits into from Jun 4, 2020
Merged

Run cleanup() when test runner supports teardown() #676

merged 3 commits into from Jun 4, 2020

Conversation

screendriver
Copy link
Contributor

What:

cleanup() now runs not only when afterEach() exists. It also runs when teardown() exists.

Why:

There are test runners that either don't have a afterEach() function or supports different UI's, like Mocha's TDD UI for example. It would be great if react-testing-library would also support teardown() for cleaning up things.

How:

I just added an additional check that checks and calls teardown() instead of beforeEach().

Checklist:

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

The main problem that I have with this pull request is:

  • linting fails because teardown is not defined. Is this somewhere deep in eslint-config-kentcdodds?
  • I had problems to write a test for that because you are using Jest which does not have teardown()

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 27, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f461860:

Sandbox Source
busy-brahmagupta-bpj0b Configuration
muddy-rgb-1h307 Configuration

src/index.js Show resolved Hide resolved
@screendriver
Copy link
Contributor Author

Now the tests fail because code coverage is below the threshold. Wich leads to my second point

  • I had problems to write a test for that because you are using Jest which does not have teardown()

@eps1lon
Copy link
Member

eps1lon commented Jun 3, 2020

I think you can tell coverage to ignore certain branches. Something like instanbul ignore-if or so. Should be able to find this via google "jest coverage ignore".

@codecov
Copy link

codecov bot commented Jun 4, 2020

Codecov Report

Merging #676 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #676   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines           99       100    +1     
  Branches        15        16    +1     
=========================================
+ Hits            99       100    +1     
Impacted Files Coverage Δ
src/index.js 100.00% <100.00%> (ø)

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 d43e278...f461860. Read the comment docs.

@screendriver
Copy link
Contributor Author

@eps1lon I ignored the else block and the build is now green

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.

Awesome! Thank you!

I wonder whether we should consider this a breaking change 😬

@screendriver
Copy link
Contributor Author

I wonder whether we should consider this a breaking change 😬

Good question. I guess not? I mean, nothing will break. It's "just" a new feature for (for example) Mocha users like me 🙃

@kentcdodds kentcdodds merged commit 7729f99 into testing-library:master Jun 4, 2020
kentcdodds pushed a commit that referenced this pull request Jun 4, 2020
* Run cleanup() when test runner supports teardown()

* Disable ESLint for teardown() function

Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>

* Ignore teardown() in code coverage because Jest does not support it

Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
@kentcdodds
Copy link
Member

@all-contributors please add @screendriver for code

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @screendriver! 🎉

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.

None yet

3 participants