Skip to content

Commit

Permalink
feat: run cleanup() when test runner supports teardown() (#676)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and kentcdodds committed Jun 4, 2020
1 parent d43e278 commit c91e9a9
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/index.js
@@ -1,14 +1,25 @@
import {cleanup} from './pure'

// if we're running in a test runner that supports afterEach
// then we'll automatically run cleanup afterEach test
// or teardown then we'll automatically run cleanup afterEach test
// this ensures that tests run in isolation from each other
// if you don't like this then either import the `pure` module
// or set the RTL_SKIP_AUTO_CLEANUP env variable to 'true'.
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_AUTO_CLEANUP) {
afterEach(async () => {
await cleanup()
})
if (!process.env.RTL_SKIP_AUTO_CLEANUP) {
// ignore teardown() in code coverage because Jest does not support it
/* istanbul ignore else */
if (typeof afterEach === 'function') {
afterEach(async () => {
await cleanup()
})
} else if (typeof teardown === 'function') {
// Block is guarded by `typeof` check.
// eslint does not support `typeof` guards.
// eslint-disable-next-line no-undef
teardown(async () => {
await cleanup()
})
}
}

export * from './pure'

0 comments on commit c91e9a9

Please sign in to comment.