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: docs/react/testing/unit-testing/setup.md
+12-55
Original file line number
Diff line number
Diff line change
@@ -8,22 +8,6 @@ description: Learn how to set up unit tests for an Ionic React application.
8
8
9
9
Ionic requires a few additional steps to set up unit tests.
10
10
11
-
## React Scripts
12
-
13
-
### Install Jest
14
-
15
-
If your React project uses `react-scripts`, then Jest is already installed. You can confirm the version of Jest by running:
16
-
17
-
```bash
18
-
npm ls jest
19
-
```
20
-
21
-
Ionic recommends `react-scripts@5` and requires a minimum version of `jest@27`, which includes `jsdom@16`. If you are using an older version of `react-scripts`, you can update it by running:
22
-
23
-
```bash
24
-
npm install react-scripts@latest
25
-
```
26
-
27
11
### Install React Testing Library
28
12
29
13
React Testing Library is a set of utilities that make it easier to test React components. It's used to interact with components and test their behavior.
@@ -38,46 +22,19 @@ Ionic React requires the `setupIonicReact` function to be called before any test
38
22
39
23
In `src/setupTest.ts`, add the following code:
40
24
41
-
```tsx title="src/setupTest.ts"
42
-
import { setupIonicReact } from'@ionic/react';
43
-
44
-
setupIonicReact();
45
-
```
46
-
47
-
### Mock `requestAnimationFrame`
48
-
49
-
Either in `src/setupTest.ts` or in the individual test file, add the following code before any tests run:
This is required if your application is using features such as `useIonModal` or `useIonPopover`.
28
+
+ import { setupIonicReact } from '@ionic/react';
66
29
67
-
:::
30
+
+ setupIonicReact();
68
31
69
-
### Test Environment
70
-
71
-
Ionic requires a DOM environment to be available in order to render components. This is typically provided by the `jsdom` test environment. In Jest 27 and later, the default environment is `node`.
72
-
73
-
To configure this behavior, update your `test` command to include `--env=jsdom`:
74
-
75
-
```json title="package.json"
76
-
"test": "react-scripts test --env=jsdom --transformIgnorePatterns 'node_modules/(?!(@ionic/react|@ionic/react-router|@ionic/core|@stencil/core|ionicons)/)'",
32
+
// Mock matchmedia
33
+
window.matchMedia = window.matchMedia || function () {
34
+
return {
35
+
matches: false,
36
+
addListener: function () { },
37
+
removeListener: function () { }
38
+
};
39
+
};
77
40
```
78
-
79
-
:::note
80
-
81
-
Starting in Ionic v6, the `--transformIgnorePatterns` flag is required.
0 commit comments