Skip to content

Commit 0f56996

Browse files
dustinmyersKent C. Dodds
authored and
Kent C. Dodds
committed
Remove cleanup references (#213)
* Remove references to cleanup-after-each This commit removes any references to the cleanup-after-each file in the react docs as it has been removed. Instructions for skipping auto cleanup were also added to the docs. * Remove references to afterEach(cleanup) This commit removes references to the afterEach function in the react docs and examples * Update docs/react-testing-library/setup.md Co-Authored-By: Kent C. Dodds <[email protected]> * Update docs/react-testing-library/setup.md Co-Authored-By: Kent C. Dodds <[email protected]> * Update docs/react-testing-library/setup.md Co-Authored-By: Kent C. Dodds <[email protected]> * Update docs/react-testing-library/setup.md Co-Authored-By: Kent C. Dodds <[email protected]> * Add cleanup section back in to api docs This commit add the cleanup section bac into the api docs for those that may need to do manual cleanups. * Update docs/react-testing-library/setup.md Co-Authored-By: Kent C. Dodds <[email protected]> * Change api doc example to AVA example The api docs now show a relevant example using the AVA testing framework. * Remove setup paragraph in api docs This commit removes the paragraph at the end of the cleanup section in the api docs that talks about configuring the cleanup globally
1 parent 97a2ef3 commit 0f56996

13 files changed

+33
-103
lines changed

docs/example-input-event.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ sidebar_label: Input Event
1212
1313
```jsx
1414
import React from 'react'
15-
import { render, fireEvent, cleanup } from '@testing-library/react'
15+
import { render, fireEvent } from '@testing-library/react'
1616

1717
class CostInput extends React.Component {
1818
state = {
@@ -49,8 +49,6 @@ const setup = () => {
4949
}
5050
}
5151

52-
afterEach(cleanup)
53-
5452
test('It should keep a $ in front of the input', () => {
5553
const { input } = setup()
5654
fireEvent.change(input, { target: { value: '23' } })

docs/example-reach-router.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Reach Router
55

66
```jsx
77
import React from 'react'
8-
import { render, cleanup } from '@testing-library/react'
8+
import { render } from '@testing-library/react'
99
import {
1010
Router,
1111
Link,
@@ -35,8 +35,6 @@ function App() {
3535

3636
// Ok, so here's what your tests might look like
3737

38-
afterEach(cleanup)
39-
4038
// this is a handy function that I would utilize for any component
4139
// that relies on the router being in context
4240
function renderWithRouter(

docs/example-react-context.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ title: React Context
55

66
```jsx
77
import React from 'react'
8-
import { render, cleanup } from '@testing-library/react'
8+
import { render } from '@testing-library/react'
99
import '@testing-library/jest-dom/extend-expect'
1010
import { NameContext, NameProvider, NameConsumer } from '../react-context'
1111

12-
afterEach(cleanup)
13-
1412
/**
1513
* Test default values by rendering a context consumer without a
1614
* matching provider

docs/example-react-hooks-useReducer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ based on the reducer state.
5757
// example.test.js
5858

5959
import React from 'react'
60-
import { render, fireEvent, cleanup } from '@testing-library/react'
60+
import { render, fireEvent } from '@testing-library/react'
6161
import Example from './example.js'
6262

6363
it('shows success message after confirm button is clicked', () => {

docs/example-react-intl.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ A complete example:
3939
```jsx
4040
import React from 'react'
4141
import '@testing-library/jest-dom/extend-expect'
42-
import { render, cleanup, getByTestId } from '@testing-library/react'
42+
import { render, getByTestId } from '@testing-library/react'
4343
import { IntlProvider, FormattedDate } from 'react-intl'
4444
import IntlPolyfill from 'intl'
4545
import 'intl/locale-data/jsonp/pt'
@@ -73,7 +73,6 @@ const renderWithReactIntl = component => {
7373
}
7474

7575
setupTests()
76-
afterEach(cleanup)
7776

7877
test('it should render FormattedDate and have a formated pt date', () => {
7978
const { container } = renderWithReactIntl(<FormatDateView />)

docs/example-react-redux.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ Now here's what your test will look like:
6565
import React from 'react'
6666
import { createStore } from 'redux'
6767
import { Provider } from 'react-redux'
68-
import { render, fireEvent, cleanup } from '@testing-library/react'
68+
import { render, fireEvent } from '@testing-library/react'
6969
import { initialState, reducer } from './reducer.js'
7070
import Counter from './counter.js'
7171

72-
afterEach(cleanup)
7372

7473
// this is a handy function that I normally make available for all my tests
7574
// that deal with connected components.

docs/example-react-router.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React from 'react'
88
import { withRouter } from 'react-router'
99
import { Link, Route, Router, Switch } from 'react-router-dom'
1010
import { createMemoryHistory } from 'history'
11-
import { render, fireEvent, cleanup } from '@testing-library/react'
11+
import { render, fireEvent } from '@testing-library/react'
1212

1313
const About = () => <div>You are on the about page</div>
1414
const Home = () => <div>You are home</div>
@@ -35,8 +35,6 @@ function App() {
3535

3636
// Ok, so here's what your tests might look like
3737

38-
afterEach(cleanup)
39-
4038
// this is a handy function that I would utilize for any component
4139
// that relies on the router being in context
4240
function renderWithRouter(

docs/example-react-transition-group.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: React Transition Group
88
```jsx
99
import React from 'react'
1010
import { CSSTransition } from 'react-transition-group'
11-
import { render, fireEvent, cleanup } from '@testing-library/react'
11+
import { render, fireEvent } from '@testing-library/react'
1212

1313
function Fade({ children, ...props }) {
1414
return (
@@ -35,8 +35,6 @@ class HiddenMessage extends React.Component {
3535
}
3636
}
3737

38-
afterEach(cleanup)
39-
4038
jest.mock('react-transition-group', () => {
4139
const FakeTransition = jest.fn(({ children }) => children)
4240
const FakeCSSTransition = jest.fn(props =>
@@ -64,7 +62,7 @@ test('you can mock things with jest.mock', () => {
6462
```jsx
6563
import React from 'react'
6664
import { CSSTransition } from 'react-transition-group'
67-
import { render, fireEvent, cleanup } from '@testing-library/react'
65+
import { render, fireEvent } from '@testing-library/react'
6866

6967
function Fade({ children, ...props }) {
7068
return (
@@ -91,8 +89,6 @@ class HiddenMessage extends React.Component {
9189
}
9290
}
9391

94-
afterEach(cleanup)
95-
9692
jest.mock('react-transition-group', () => {
9793
const FakeCSSTransition = jest.fn(() => null)
9894
return { CSSTransition: FakeCSSTransition }

docs/example-update-props.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ sidebar_label: Update Props
1010
// that your first call created for you.
1111

1212
import React from 'react'
13-
import { render, cleanup } from '@testing-library/react'
13+
import { render } from '@testing-library/react'
1414

1515
let idCounter = 1
1616

@@ -26,8 +26,6 @@ class NumberDisplay extends React.Component {
2626
}
2727
}
2828

29-
afterEach(cleanup)
30-
3129
test('calling render with the same component on the same container does not remount', () => {
3230
const { getByTestId, rerender } = render(<NumberDisplay number={1} />)
3331
expect(getByTestId('number-display').textContent).toBe('1')

docs/react-testing-library/api.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ render(<div />)
3232
```
3333

3434
```jsx
35-
import { render, cleanup } from '@testing-library/react'
35+
import { render } from '@testing-library/react'
3636
import '@testing-library/jest-dom/extend-expect'
37-
afterEach(cleanup)
3837
3938
test('renders a message', () => {
4039
const { container, getByText } = render(<Greeting />)
@@ -45,11 +44,6 @@ test('renders a message', () => {
4544
})
4645
```
4746

48-
> Note
49-
>
50-
> The [cleanup](#cleanup) function should be called between tests to remove the
51-
> created DOM nodes and keep the tests isolated.
52-
5347
## `render` Options
5448

5549
You won't often need to specify options, but if you ever do, here are the
@@ -256,10 +250,18 @@ expect(firstRender).toMatchDiffSnapshot(asFragment())
256250
257251
Unmounts React trees that were mounted with [render](#render).
258252
253+
> Please note that this is done automatically if the testing framework you're
254+
> using supports the `afterEach` global (like mocha, Jest, and Jasmine). If not,
255+
> you will need to do manual cleanups after each test.
256+
257+
For example, if you're using the [ava](https://github.com/avajs/ava) testing
258+
framework, then you would need to use the `test.afterEach` hook like so:
259+
259260
```jsx
260261
import { cleanup, render } from '@testing-library/react'
262+
import test from 'ava'
261263

262-
afterEach(cleanup) // <-- add this
264+
test.afterEach(cleanup)
263265

264266
test('renders into document', () => {
265267
render(<div />)
@@ -273,11 +275,6 @@ Failing to call `cleanup` when you've called `render` could result in a memory
273275
leak and tests which are not "idempotent" (which can lead to difficult to debug
274276
errors in your tests).
275277
276-
**If you don't want to add this to _every single test file_** then we recommend
277-
that you configure your test framework to run a file before your tests which
278-
does this automatically. See the [setup](./setup) section for guidance on how to
279-
set up your framework.
280-
281278
---
282279
283280
## `act`

docs/react-testing-library/example-intro.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ import React from 'react'
1414
import {
1515
render,
1616
fireEvent,
17-
cleanup,
1817
waitForElement,
1918
} from '@testing-library/react'
2019
import '@testing-library/jest-dom/extend-expect'
2120
import axiosMock from 'axios'
2221
import Fetch from '../fetch'
2322

24-
afterEach(cleanup)
25-
2623
test('loads and displays greeting', async () => {
2724
const url = '/greeting'
2825
const { getByText, getByTestId } = render(<Fetch url={url} />)
@@ -58,7 +55,6 @@ import React from 'react'
5855
import {
5956
render,
6057
fireEvent,
61-
cleanup,
6258
waitForElement,
6359
} from '@testing-library/react'
6460

@@ -74,9 +70,6 @@ import Fetch from '../fetch'
7470
```
7571

7672
```jsx
77-
// automatically unmount and cleanup DOM after the test is finished.
78-
afterEach(cleanup)
79-
8073
test('loads and displays greeting', async () => {
8174
// Arrange
8275
// Act

docs/react-testing-library/faq.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ In summary:
1717

1818
```javascript
1919
import React from 'react'
20-
import '@testing-library/react/cleanup-after-each'
2120
import { render, fireEvent } from '@testing-library/react'
2221

2322
test('change values via the fireEvent.change method', () => {

docs/react-testing-library/setup.md

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,6 @@ does not require that you use Jest).
1616
Adding options to your global test config can simplify the setup and teardown of
1717
tests in individual files.
1818

19-
### Cleanup
20-
21-
You can ensure [`cleanup`](./api#cleanup) is called after each test and import
22-
additional assertions by adding it to the setup configuration in Jest.
23-
24-
In Jest 24 and up, add the
25-
[`setupFilesAfterEnv`](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)
26-
option to your Jest config:
27-
28-
```javascript
29-
// jest.config.js
30-
module.exports = {
31-
setupFilesAfterEnv: [
32-
'@testing-library/react/cleanup-after-each',
33-
// ... other setup files ...
34-
],
35-
// ... other options ...
36-
}
37-
```
38-
39-
<details>
40-
41-
<summary>Older versions of Jest</summary>
42-
43-
Jest versions 23 and below use the
44-
[`setupTestFrameworkScriptFile`](https://jestjs.io/docs/en/23.x/configuration#setuptestframeworkscriptfile-string)
45-
option in your Jest config instead of `setupFilesAfterEnv`. This setup file can
46-
be anywhere, for example `jest.setup.js` or `./utils/setupTests.js`.
47-
48-
If you are using the default setup from create-react-app, this option is set to
49-
`src/setupTests.js`. You should create this file if it doesn't exist and put the
50-
setup code there.
51-
52-
```javascript
53-
// jest.config.js
54-
module.exports = {
55-
setupTestFrameworkScriptFile: require.resolve('./jest.setup.js'),
56-
// ... other options ...
57-
}
58-
```
59-
60-
```javascript
61-
// jest.setup.js
62-
63-
// add some helpful assertions
64-
import '@testing-library/jest-dom/extend-expect'
65-
66-
// this is basically: afterEach(cleanup)
67-
import '@testing-library/react/cleanup-after-each'
68-
```
69-
70-
</details>
71-
7219
## Custom Render
7320

7421
It's often useful to define a custom render method that includes things like
@@ -257,7 +204,7 @@ module.exports = {
257204
}
258205
```
259206

260-
*Typescript config needs to be updated as follow:*
207+
_Typescript config needs to be updated as follow:_
261208

262209
```diff
263210
// tsconfig.json
@@ -272,8 +219,6 @@ module.exports = {
272219
}
273220
```
274221

275-
276-
277222
### Jest and Create React App
278223

279224
If your project is based on top of Create React App, to make the `test-utils`
@@ -328,3 +273,15 @@ mocha --require jsdom-global/register
328273
Note, depending on the version of Node you're running, you may also need to
329274
install @babel/polyfill (if you're using babel 7) or babel-polyfill (for babel
330275
6).
276+
277+
### Skipping Auto Cleanup
278+
279+
[`Cleanup`](./api#cleanup) is called after each test automatically by default if
280+
the testing framework you're using supports the `afterEach` global (like mocha,
281+
Jest, and Jasmine). However, you may choose to skip the auto cleanup by setting
282+
the `RTL_SKIP_AUTO_CLEANUP` env variable to 'true'. You can do this with
283+
[`cross-env`](https://github.com/kentcdodds/cross-env) like so:
284+
285+
```
286+
cross-env RTL_SKIP_AUTO_CLEANUP=true jest
287+
```

0 commit comments

Comments
 (0)