From 4626ba120a1c43a84607169a469995d9fdc402a0 Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Wed, 27 May 2020 08:04:43 +0200 Subject: [PATCH 1/3] Run cleanup() when test runner supports teardown() --- src/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 38aa9076..538a14dd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,20 @@ 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) { + if (typeof afterEach === 'function') { + afterEach(async () => { + await cleanup() + }) + } else if (typeof teardown === 'function') { + teardown(async () => { + await cleanup() + }) + } } export * from './pure' From aa56c114cc808b50c145a6413f8a04869469c5ee Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Wed, 3 Jun 2020 11:00:06 +0200 Subject: [PATCH 2/3] Disable ESLint for teardown() function Co-authored-by: Sebastian Silbermann --- src/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.js b/src/index.js index 538a14dd..c5f5ff32 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,9 @@ if (!process.env.RTL_SKIP_AUTO_CLEANUP) { 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() }) From f461860bf80e1aaacb0c8ee5f9b4a9fd94658c4f Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Thu, 4 Jun 2020 06:40:02 +0200 Subject: [PATCH 3/3] Ignore teardown() in code coverage because Jest does not support it --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index c5f5ff32..b69f6555 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,8 @@ import {cleanup} from './pure' // if you don't like this then either import the `pure` module // or set the RTL_SKIP_AUTO_CLEANUP env variable to 'true'. 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()