Skip to content
This repository was archived by the owner on Aug 1, 2020. It is now read-only.

Commit 0e7e29c

Browse files
committed
docs(cleanup): add docs about cleanup
1 parent c36d3f9 commit 0e7e29c

File tree

6 files changed

+100
-10
lines changed

6 files changed

+100
-10
lines changed

docs/api-main.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,13 @@ test('renders into document', () => {
168168
// ... more tests ...
169169
```
170170

171+
In native-testing-library there is no DOM to cleanup, and your tests' rendered trees cannot
172+
interfere with each other. It is simply nice to be able to run any unmount logic in the components
173+
you have rendered in your tests.
174+
171175
**If you don't want to add this to _every single test file_** then we recommend that you configure
172-
your test framework to run a file before your tests which does this automatically.
176+
your test framework to run a file before your tests which does this automatically. See the
177+
[setup](./setup) section for guidance on how to set up your framework.
173178

174179
---
175180

docs/ecosystem-jest-native.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ npm install --save-dev jest-native
1212
```
1313

1414
```javascript
15-
<View>
16-
<View testID="not-empty">
17-
<Text testID="empty" />
18-
</View>
19-
<Text testID="visible">Visible Example</Text>
20-
</View>;
15+
const { getByText, getByTestId } = render(
16+
<View>
17+
<View testID="not-empty">
18+
<Text testID="empty" />
19+
</View>
20+
<Text testID="visible">Text Example</Text>
21+
</View>,
22+
);
2123

22-
expect(queryByTestId(baseElement, 'not-empty')).not.toBeEmpty();
23-
expect(getByText(baseElement, 'Visible Example')).toBeVisible();
24+
expect(getByTestId('not-empty')).not.toBeEmpty();
25+
expect(getByTestId('empty')).toBeEmpty();
26+
expect(getByText('Visible Example')).toHaveTextContent('Text Example');
2427
```
2528

2629
> Note: when using some of these matchers, you may need to make sure you use a query function (like

docs/setup.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ preset should also work, but you'll be getting the best experience when using ou
1515
be some additional mocks you need to provide to such as mocks for `react-native-gesture-handler`
1616
when using `react-navigation`.
1717

18+
## Cleanup
19+
20+
You can ensure [`cleanup`](./api-main#cleanup) is called after each test and import additional
21+
assertions by adding it to the setup configuration in Jest.
22+
23+
In Jest 24 and up, add the
24+
[`setupFilesAfterEnv`](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array) option
25+
to your Jest config:
26+
27+
```javascript
28+
// jest.config.js
29+
module.exports = {
30+
setupFilesAfterEnv: [
31+
'native-testing-library/cleanup-after-each',
32+
// ... other setup files ...
33+
],
34+
// ... other options ...
35+
};
36+
```
37+
1838
## Custom Render
1939

2040
It's often useful to define a custom render method that includes things like global context

website/versioned_docs/version-3.0.0/api-main.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,13 @@ test('renders into document', () => {
169169
// ... more tests ...
170170
```
171171

172+
In native-testing-library there is no DOM to cleanup, and your tests' rendered trees cannot
173+
interfere with each other. It is simply nice to be able to run any unmount logic in the components
174+
you have rendered in your tests.
175+
172176
**If you don't want to add this to _every single test file_** then we recommend that you configure
173-
your test framework to run a file before your tests which does this automatically.
177+
your test framework to run a file before your tests which does this automatically. See the
178+
[setup](./setup) section for guidance on how to set up your framework.
174179

175180
---
176181

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
id: version-3.0.0-ecosystem-jest-native
3+
title: Jest Matchers
4+
sidebar_label: Jest Matchers
5+
original_id: ecosystem-jest-native
6+
---
7+
8+
[`jest-native`](https://github.com/testing-library/jest-native) is a companion library for
9+
`native-testing-library` that provides custom element matchers for Jest.
10+
11+
```
12+
npm install --save-dev jest-native
13+
```
14+
15+
```javascript
16+
const { getByText, getByTestId } = render(
17+
<View>
18+
<View testID="not-empty">
19+
<Text testID="empty" />
20+
</View>
21+
<Text testID="visible">Text Example</Text>
22+
</View>,
23+
);
24+
25+
expect(getByTestId('not-empty')).not.toBeEmpty();
26+
expect(getByTestId('empty')).toBeEmpty();
27+
expect(getByText('Visible Example')).toHaveTextContent('Text Example');
28+
```
29+
30+
> Note: when using some of these matchers, you may need to make sure you use a query function (like
31+
> `queryByTestId`) rather than a get function (like `getByTestId`). Otherwise the `get*` function
32+
> could throw an error before your assertion.
33+
34+
Check out [jest-native's documentation](https://github.com/testing-library/jest-native) for a full
35+
list of available matchers.
36+
37+
- [jest-native on GitHub](https://github.com/testing-library/jest-native)

website/versioned_docs/version-3.0.0/setup.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ preset should also work, but you'll be getting the best experience when using ou
1616
be some additional mocks you need to provide to such as mocks for `react-native-gesture-handler`
1717
when using `react-navigation`.
1818

19+
## Cleanup
20+
21+
You can ensure [`cleanup`](./api-main#cleanup) is called after each test and import additional
22+
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) option
26+
to your Jest config:
27+
28+
```javascript
29+
// jest.config.js
30+
module.exports = {
31+
setupFilesAfterEnv: [
32+
'native-testing-library/cleanup-after-each',
33+
// ... other setup files ...
34+
],
35+
// ... other options ...
36+
};
37+
```
38+
1939
## Custom Render
2040

2141
It's often useful to define a custom render method that includes things like global context

0 commit comments

Comments
 (0)