Skip to content

Commit 45ca61c

Browse files
committed
docs: add "cleanup" section
1 parent 9400026 commit 45ca61c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/API.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,45 @@ toJSON(): ReactTestRendererJSON | null
115115

116116
Get the rendered component JSON representation, e.g. for snapshot testing.
117117

118+
## `cleanup`
119+
120+
```ts
121+
const cleanup: () => void
122+
```
123+
124+
Unmounts React trees that were mounted with `render`.
125+
126+
For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:
127+
128+
```jsx
129+
import { cleanup, render } from 'react-native-testing-library'
130+
import { View } from 'react-native'
131+
132+
afterEach(cleanup)
133+
134+
it('renders a view', () => {
135+
render(<View />)
136+
// ...
137+
})
138+
```
139+
140+
The `afterEach(cleanup)` call also works in `describe` blocks:
141+
142+
```jsx
143+
describe('when logged in', () => {
144+
afterEach(cleanup)
145+
146+
it('renders the user', () => {
147+
render(<SiteHeader />)
148+
// ...
149+
});
150+
})
151+
```
152+
153+
Failing to call `cleanup` when you've called `render` could result in a memory leak and tests which are not "idempotent" (which can lead to difficult to debug errors in your tests).
154+
155+
The alternative to `cleanup` is balancing every `render` with an `unmount` method call.
156+
118157
## `fireEvent`
119158

120159
```ts

0 commit comments

Comments
 (0)