@@ -8,19 +8,22 @@ sidebar_label: Setup
8
8
demonstrated in the example above). However, there are some things you can do
9
9
when configuring your testing framework to reduce some boilerplate. In these
10
10
docs we'll demonstrate configuring Jest, but you should be able to do similar
11
- things with any testing framework (react-testing-library does not require that
12
- you use Jest).
11
+ things with [ any testing framework] ( #using-without-jest ) (react-testing-library
12
+ does not require that you use Jest).
13
13
14
14
## Global Config
15
15
16
- There are several options you can add to your global test config that simplify
17
- the setup and teardown of tests in individual files. For example, you can ensure
18
- [ ` cleanup ` ] ( ./api#cleanup ) is called after each test and import additional
19
- assertions.
16
+ Adding options to your global test config can simplify the setup and teardown of
17
+ tests in individual files.
20
18
21
- To do this with Jest 24 and up, you can add the
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
22
25
[ ` setupFilesAfterEnv ` ] ( https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array )
23
- option to your Jest config.
26
+ option to your Jest config:
24
27
25
28
``` javascript
26
29
// jest.config.js
@@ -33,10 +36,10 @@ module.exports = {
33
36
}
34
37
```
35
38
36
- ### Older versions of Jest
37
-
38
39
<details >
39
40
41
+ <summary >Older versions of Jest</summary >
42
+
40
43
Jest versions 23 and below use the
41
44
[ ` setupTestFrameworkScriptFile ` ] ( https://jestjs.io/docs/en/23.x/configuration#setuptestframeworkscriptfile-string )
42
45
option in your Jest config instead of ` setupFilesAfterEnv ` . This setup file can
@@ -72,7 +75,11 @@ It's often useful to define a custom render method that includes things like
72
75
global context providers, data stores, etc. To make this available globally, one
73
76
approach is to define a utility file that re-exports everything from
74
77
` react-testing-library ` . You can replace react-testing-library with this file in
75
- all your imports.
78
+ all your imports. See [ below] ( #comfiguring-jest-with-test-utils ) for a way to
79
+ make your test util file accessible without using relative paths.
80
+
81
+ The example below sets up data providers using the
82
+ [ ` wrapper ` ] ( api.md#render-options ) option to ` render ` .
76
83
77
84
``` diff
78
85
// my-component.test.js
@@ -87,27 +94,64 @@ import { ThemeProvider } from 'my-ui-lib'
87
94
import { TranslationProvider } from ' my-i18n-lib'
88
95
import defaultStrings from ' i18n/en-x-default'
89
96
90
- const customRender = (node , options ) => {
91
- return render (
97
+ const AllTheProviders = ({ children } ) => {
98
+ return (
92
99
< ThemeProvider theme= " light" >
93
100
< TranslationProvider messages= {defaultStrings}>
94
- {node }
101
+ {children }
95
102
< / TranslationProvider>
96
- < / ThemeProvider> ,
97
- options
103
+ < / ThemeProvider>
98
104
)
99
105
}
100
106
107
+ const customRender = (ui , options ) =>
108
+ render (ui, { wrapper: AllTheProviders, ... options })
109
+
101
110
// re-export everything
102
111
export * from ' react-testing-library'
103
112
104
113
// override render method
105
114
export { customRender as render }
106
115
```
107
116
108
- To make this file accessible without using relative imports, add the folder
109
- containing the file to the Jest ` moduleDirectories ` option. Note: this will make
110
- _ all_ the .js files in that directory importable without ` ../ ` .
117
+ > ** Note**
118
+ >
119
+ > Babel versions lower than 7 throw an error when trying to override the named
120
+ > export in the example above. See
121
+ > [ #169 ] ( https://github.com/kentcdodds/react-testing-library/issues/169 ) and the
122
+ > workaround below.
123
+
124
+ <details >
125
+ <summary >Workaround for Babel 6</summary >
126
+
127
+ You can use CommonJS modules instead of ES modules, which should work in Node:
128
+
129
+ ``` js
130
+ // test-utils.js
131
+ const rtl = require (' react-testing-library' )
132
+
133
+ const customRender = (ui , options ) =>
134
+ rtl .render (ui, {
135
+ myDefaultOption: ' something' ,
136
+ ... options,
137
+ })
138
+
139
+ module .exports = {
140
+ ... rtl,
141
+ render: customRender,
142
+ }
143
+ ```
144
+
145
+ </details >
146
+
147
+ ### Configuring Jest with Test Utils
148
+
149
+ To make your custom test file accessible in your Jest test files without using
150
+ relative imports (` ../../test-utils ` ), add the folder containing the file to the
151
+ Jest ` moduleDirectories ` option.
152
+
153
+ This will make all the ` .js ` files in the test-utils directory importable
154
+ without ` ../ ` .
111
155
112
156
``` diff
113
157
// my-component.test.js
@@ -128,9 +172,11 @@ module.exports = {
128
172
}
129
173
```
130
174
131
- If your project is based on top of Create React App, to make the file accessible
132
- without using relative imports, you just need to create a ` .env ` file in the
133
- root of your project with the following configuration:
175
+ ### Jest and Create React App
176
+
177
+ If your project is based on top of Create React App, to make the ` test-utils `
178
+ file accessible without using relative imports, you just need to create a ` .env `
179
+ file in the root of your project with the following configuration:
134
180
135
181
```
136
182
// Create React App project structure
@@ -151,54 +197,6 @@ $ app
151
197
NODE_PATH=src/utils
152
198
```
153
199
154
- There is the case when you want to wrap your components in a ` Provider ` , this
155
- might cause conflicts when ` rerender ` ed. To achieve this, we suggest the
156
- ` rerender ` should be implemented the same way custom queries, by changing the
157
- return value of the customRender.
158
-
159
- ``` js
160
- // test-utils.js
161
-
162
- const customRender = (ui , options ) => {
163
- const rendered = render (< div> {ui}< / div> , options)
164
- return {
165
- ... rendered,
166
- rerender : newUi =>
167
- customRender (newUi, {
168
- container: rendered .container ,
169
- baseElement: rendered .baseElement ,
170
- }),
171
- }
172
- }
173
- ```
174
-
175
- ### Export Issue with Babel Versions Lower Than 7
176
-
177
- Babel versions lower than 7 throw an error when trying to override the named
178
- export in the example above. (See
179
- [ #169 ] ( https://github.com/kentcdodds/react-testing-library/issues/169 ) .)
180
-
181
- <details >
182
- <summary >Workaround</summary >
183
-
184
- You can use CommonJS modules instead of ES modules, which should work in Node:
185
-
186
- ``` js
187
- // test-utils.js
188
- const rtl = require (' react-testing-library' )
189
-
190
- const customRender = (node , options ) => {
191
- return rtl .render (< Something> {node}< / Something> )
192
- }
193
-
194
- module .exports = {
195
- ... rtl,
196
- render: customRender,
197
- }
198
- ```
199
-
200
- </details >
201
-
202
200
## Using without Jest
203
201
204
202
If you're running your tests in the browser bundled with webpack (or similar)
0 commit comments