You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: developer_docs/testing.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,27 +30,27 @@ Many files still don't have tests, so if you're looking to get started as a cont
30
30
## Useful testing commands
31
31
Run the whole test suite
32
32
```
33
-
npm run test
33
+
$ npm run test
34
34
```
35
35
36
36
-----
37
37
38
38
[Run tests that match the pattern](https://stackoverflow.com/questions/28725955/how-do-i-test-a-single-file-using-jest/28775887). Useful if you're writing one specific test and want to only run that one.
39
39
```
40
-
npm run test -- someFileName
40
+
$ npm run test -- someFileName
41
41
```
42
42
43
43
-----
44
44
Run the tests but update the snapshot if they don't match.
45
45
46
46
```
47
-
npm run test -- -u
47
+
$ npm run test -- -u
48
48
```
49
49
50
50
----
51
51
For example, if you wanted to run just the SketchList test but also update the snapshot, you could do:
52
52
```
53
-
npm run test -- Sketchlist.test.js -u
53
+
$ npm run test -- Sketchlist.test.js -u
54
54
```
55
55
56
56
Find more commands in the [Jest documentation](https://jestjs.io/docs/cli).
@@ -81,7 +81,7 @@ Want to get started writing a test for a new file or an existing file, but not s
81
81
### What to test
82
82
For any type of component, you might want to consider testing:
83
83
- User input results in the class's method being called.
84
-
```
84
+
```js
85
85
//component is the return value of calling render()
@@ -99,18 +99,18 @@ See the [redux section](#Testing-Redux) below :)
99
99
### Troubleshooting
100
100
1. Check if the component makes any API calls. If it's using axios, jest should already be set up to replace the axios library with a mocked version; however, you may want to [mock](https://jestjs.io/docs/mock-function-api#mockfnmockimplementationoncefn) the axios.get() function with your own version so that GET calls "return" whatever data makes sense for that test.
101
101
102
-
```
102
+
```js
103
103
axios.get.mockImplementationOnce(
104
104
(x) =>Promise.resolve({ data:'foo' })
105
105
);
106
106
```
107
-
You can see it used in the context of a test [here](../client/modules/IDE/components/SketchList.test.jsx).
107
+
You can see it used in the context of a test [in the SketchList.test.jsx file](../client/modules/IDE/components/SketchList.test.jsx).
108
108
109
109
2. If the component makes use of the formatDate util, some of the functions in that rely on the ``./client/i18n.js`` file that also makes an ajax request, which sometimes leads to an ERRCONNECTED error on the console, even though your tests pass. You can fix it by adding a mock for that specific i18n file:
110
-
```
110
+
```js
111
111
jest.mock('_path_to_file_/i18n');
112
112
```
113
-
You can see it used in the context of a test [here](../client/modules/IDE/components/SketchList.test.jsx).
113
+
You can also see it used in the context of a test [in the SketchList.test.jsx file](../client/modules/IDE/components/SketchList.test.jsx).
114
114
115
115
## Files to be aware of
116
116
@@ -218,19 +218,19 @@ It exports a render function with a i18n wrapper as ``render`` and a render func
218
218
Thus, in your component test files, instead of calling ``import {functions you want} from 'react-testing-libary'`` importing react-testing library might look something like this:
219
219
220
220
If your component only needs i18n and not redux:
221
-
```
221
+
```js
222
222
import { render, fireEvent, screen, waitFor } from '../../../test-utils';
223
223
```
224
224
If your component needs i18n and redux:
225
-
```
225
+
```js
226
226
import { reduxRender, fireEvent, screen, waitFor } from '../../../test-utils';
227
227
```
228
228
229
229
Redux and i18next are made accessible by placing wrappers around the component. We can do this by replacing the render function with one that renders the requested component WITH an additional wrapper added around it.
230
230
231
231
For example, the exported render function that adds a wrapper for both redux and i18n looks roughly like this:
232
232
233
-
```
233
+
```js
234
234
functionreduxRender(
235
235
ui,
236
236
{
@@ -264,7 +264,7 @@ in progress
264
264
## Testing plain components
265
265
If it doesn't contain ``connect(mapStateToProps, mapDispatchToProps)(ComponentName)`` or use hooks like ``useSelector``, then your component is not directly using Redux and testing your component will be simpler and might look something like this:
0 commit comments