From f676d9640f6cfe53615c7c5ee25f5a724471520b Mon Sep 17 00:00:00 2001 From: Matheus Marques Date: Tue, 19 Mar 2019 10:27:47 -0300 Subject: [PATCH 1/2] Added global cleanup Closes #13 --- .all-contributorsrc | 9 +++++++++ README.md | 4 ++-- cleanup-after-each.js | 1 + package.json | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 cleanup-after-each.js diff --git a/.all-contributorsrc b/.all-contributorsrc index 07762532..b7349743 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -43,6 +43,15 @@ "ideas", "review" ] + }, + { + "login": "marquesm91", + "name": "Matheus Marques", + "avatar_url": "https://avatars3.githubusercontent.com/u/7120471?v=4", + "profile": "https://twitter.com/matiosfm", + "contributions": [ + "code" + ] } ] } diff --git a/README.md b/README.md index c732693d..c2080420 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ [![downloads](https://img.shields.io/npm/dm/react-hooks-testing-library.svg?style=flat-square)](http://www.npmtrends.com/react-hooks-testing-library) [![MIT License](https://img.shields.io/npm/l/react-hooks-testing-library.svg?style=flat-square)](https://github.com/mpeyper/react-hooks-testing-library/blob/master/LICENSE.md) -[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/mpeyper/react-hooks-testing-library/blob/master/CODE_OF_CONDUCT.md) @@ -158,7 +158,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds -
Michael Peyper
Michael Peyper

💻 🎨 📖 🤔 🚇 📦 ⚠️ 🔧
otofu-square
otofu-square

💻
Patrick P. Henley
Patrick P. Henley

🤔 👀
+
Michael Peyper
Michael Peyper

💻 🎨 📖 🤔 🚇 📦 ⚠️ 🔧
otofu-square
otofu-square

💻
Patrick P. Henley
Patrick P. Henley

🤔 👀
Matheus Marques
Matheus Marques

💻
diff --git a/cleanup-after-each.js b/cleanup-after-each.js new file mode 100644 index 00000000..2617fe93 --- /dev/null +++ b/cleanup-after-each.js @@ -0,0 +1 @@ +afterEach(require('./lib').cleanup) diff --git a/package.json b/package.json index 25573b6c..ac9bfd9f 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "lib", "src", "typings", + "cleanup-after-each.js", "README.md", "LICENSE.md" ], From 90eb946bfe9b1391b8618646be1a4c9bd9664b4b Mon Sep 17 00:00:00 2001 From: Matheus Marques Date: Thu, 21 Mar 2019 20:39:21 -0300 Subject: [PATCH 2/2] docs: add global cleanup instructions --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index c2080420..d15485f9 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,22 @@ Unmounts any React trees that were mounted with [renderHook](#renderhookcallback This is the same [`cleanup` function](https://testing-library.com/docs/react-testing-library/api#cleanup) that is exported by `react-testing-library`. +Optionally, it is possible to import `cleanup` in a global test file. Using that way, it isn't necessary to run `afterEach(cleanup)` on every test script. + +```js +// in package.json +"jest": { + // ... + // use this if Jest version < 24 + "setupTestFrameworkScriptFile": "/src/setupTests.js", + // or if Jest version >= 24 + "setupFilesAfterEnv": ["/src/setupTests.js"], +} + +// src/setupTests.js +import 'react-hooks-testing-library/cleanup-after-each'; +``` + ### `act(callback)` This is the same [`act` function](https://testing-library.com/docs/react-testing-library/api#act) that is exported by `react-testing-library`.