Skip to content

Commit 7729f99

Browse files
feat; run cleanup() when test runner supports teardown() (#676)
* 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>
1 parent d43e278 commit 7729f99

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import {cleanup} from './pure'
22

33
// if we're running in a test runner that supports afterEach
4-
// then we'll automatically run cleanup afterEach test
4+
// or teardown then we'll automatically run cleanup afterEach test
55
// this ensures that tests run in isolation from each other
66
// if you don't like this then either import the `pure` module
77
// or set the RTL_SKIP_AUTO_CLEANUP env variable to 'true'.
8-
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_AUTO_CLEANUP) {
9-
afterEach(async () => {
10-
await cleanup()
11-
})
8+
if (!process.env.RTL_SKIP_AUTO_CLEANUP) {
9+
// ignore teardown() in code coverage because Jest does not support it
10+
/* istanbul ignore else */
11+
if (typeof afterEach === 'function') {
12+
afterEach(async () => {
13+
await cleanup()
14+
})
15+
} else if (typeof teardown === 'function') {
16+
// Block is guarded by `typeof` check.
17+
// eslint does not support `typeof` guards.
18+
// eslint-disable-next-line no-undef
19+
teardown(async () => {
20+
await cleanup()
21+
})
22+
}
1223
}
1324

1425
export * from './pure'

0 commit comments

Comments
 (0)