From c91e9a986a41d8663822e90a9c901e4cc6f2ee7a Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Thu, 4 Jun 2020 15:46:55 +0200 Subject: [PATCH] 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 * Ignore teardown() in code coverage because Jest does not support it Co-authored-by: Sebastian Silbermann --- src/index.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 38aa9076..b69f6555 100644 --- a/src/index.js +++ b/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'