Skip to content

Commit e63127b

Browse files
committed
chore: update based on vitest
1 parent 6c94344 commit e63127b

File tree

2 files changed

+14
-63
lines changed

2 files changed

+14
-63
lines changed

docs/react/testing/unit-testing/examples.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ test('button presents a modal when clicked', async () => {
5353

5454
This example shows how to test a modal that is presented using the `useIonModal` hook. The modal is presented when the user clicks a button.
5555

56-
:::warning
57-
58-
You must mock `requestAnimationFrame` as described in the [setup section](./setup.md#mock-requestanimationframe).
59-
60-
:::
61-
6256
### Example component
6357

6458
```tsx title="src/Example.tsx"
@@ -101,7 +95,7 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
10195

10296
import Example from './Example';
10397

104-
test('should present ModalContent when button is clicked', () => {
98+
test('should present ModalContent when button is clicked', async () => {
10599
render(
106100
<IonApp>
107101
<Example />
@@ -112,7 +106,7 @@ test('should present ModalContent when button is clicked', () => {
112106
// Wait for the modal to be presented
113107
await waitFor(() => {
114108
// Assert that the modal is present
115-
expect(screen.getByText('Modal content')).toBeInTheDocument();
109+
expect(screen.getByText('Modal Content')).toBeInTheDocument();
116110
});
117111
});
118112
```

docs/react/testing/unit-testing/setup.md

+12-55
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,6 @@ description: Learn how to set up unit tests for an Ionic React application.
88

99
Ionic requires a few additional steps to set up unit tests.
1010

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-
2711
### Install React Testing Library
2812

2913
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
3822

3923
In `src/setupTest.ts`, add the following code:
4024

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:
50-
51-
```tsx title="src/setupTest.ts"
52-
beforeEach(() => {
53-
jest.useFakeTimers();
54-
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb());
55-
});
56-
57-
afterEach(() => {
58-
window.requestAnimationFrame.mockRestore();
59-
jest.clearAllMocks();
60-
});
61-
```
62-
63-
:::note
25+
```diff
26+
import '@testing-library/jest-dom/extend-expect';
6427

65-
This is required if your application is using features such as `useIonModal` or `useIonPopover`.
28+
+ import { setupIonicReact } from '@ionic/react';
6629

67-
:::
30+
+ setupIonicReact();
6831

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+
};
7740
```
78-
79-
:::note
80-
81-
Starting in Ionic v6, the `--transformIgnorePatterns` flag is required.
82-
83-
:::

0 commit comments

Comments
 (0)