Skip to content

docs: update for Jest 29 #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions website/docs/advanced/Jest-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ title: Jest integration
sidebar_label: Jest integration
---


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.
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.

## Using Async Storage mock

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

### With __mocks__ directory
### With **mocks** directory

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

```javascript
export default from '@react-native-async-storage/async-storage/jest/async-storage-mock'
export default from '@react-native-async-storage/async-storage/jest/async-storage-mock';
```

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

```javascript
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';

jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock')
);
```

## Testing with mock

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:
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:

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

expect(AsyncStorage.getItem).toBeCalledWith('myKey');
})
});
```

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

export default AsyncStorageMock;
```

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.
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.