Skip to content

Commit 1736e37

Browse files
authored
docs: update for Jest 29 (#865)
1 parent 409b150 commit 1736e37

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

website/docs/advanced/Jest-integration.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ title: Jest integration
44
sidebar_label: Jest integration
55
---
66

7-
8-
Async Storage module is tightly coupled with its `NativeModule` part - it needs a running React Native application to work properly. In order to use it in tests, you have to provide its separate implementation. Follow these steps to add a mocked `Async Storage` module.
7+
Async Storage module is tightly coupled with its `NativeModule` part - it needs
8+
a running React Native application to work properly. In order to use it in
9+
tests, you have to provide its separate implementation. Follow these steps to
10+
add a mocked `Async Storage` module.
911

1012
## Using Async Storage mock
1113

1214
You can use one of two ways to provide mocked version of `AsyncStorage`:
1315

14-
### With __mocks__ directory
16+
### With **mocks** directory
1517

16-
1. In your project root directory, create `__mocks__/@react-native-async-storage` directory.
18+
1. In your project root directory, create
19+
`__mocks__/@react-native-async-storage` directory.
1720
2. Inside that folder, create `async-storage.js` file.
1821
3. Inside that file, export `Async Storage` mock.
1922

2023
```javascript
21-
export default from '@react-native-async-storage/async-storage/jest/async-storage-mock'
24+
export default from '@react-native-async-storage/async-storage/jest/async-storage-mock';
2225
```
2326

2427
### With Jest setup file
@@ -34,20 +37,24 @@ export default from '@react-native-async-storage/async-storage/jest/async-storag
3437
2. Inside your setup file, set up Async Storage mocking:
3538

3639
```javascript
37-
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';
38-
39-
jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
40+
jest.mock('@react-native-async-storage/async-storage', () =>
41+
require('@react-native-async-storage/async-storage/jest/async-storage-mock')
42+
);
4043
```
44+
4145
## Testing with mock
4246

43-
Each public method available from `Async Storage` is [a mock function](https://jestjs.io/docs/en/mock-functions), that you can test for certain condition, for example, if `.getItem` has been called with a specific arguments:
47+
Each public method available from `Async Storage` is
48+
[a mock function](https://jestjs.io/docs/en/mock-functions), that you can test
49+
for certain condition, for example, if `.getItem` has been called with a
50+
specific arguments:
4451

4552
```javascript
4653
it('checks if Async Storage is used', async () => {
4754
await asyncOperationOnAsyncStorage();
4855

4956
expect(AsyncStorage.getItem).toBeCalledWith('myKey');
50-
})
57+
});
5158
```
5259

5360
## Overriding Mock logic
@@ -61,9 +68,11 @@ import AsyncStorageMock from '@react-native-async-storage/async-storage/jest/asy
6168
AsyncStorageMock.multiGet = jest.fn(([keys], callback) => {
6269
// do something here to retrieve data
6370
callback([]);
64-
})
71+
});
6572

6673
export default AsyncStorageMock;
6774
```
6875

69-
You can [check its implementation](https://github.com/react-native-async-storage/async-storage/blob/master/jest/async-storage-mock.js) to get more insight into methods signatures.
76+
You can
77+
[check its implementation](https://github.com/react-native-async-storage/async-storage/blob/master/jest/async-storage-mock.js)
78+
to get more insight into methods signatures.

0 commit comments

Comments
 (0)