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
Async Storage module is tighly coupled with a `Native Module`, meaning 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 those steps to add mocked `Async Storage`to your test cases.
3
+
Async Storage module is tighly 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 those steps to add a mocked `Async Storage`module.
4
4
5
5
## Using Async Storage mock
6
6
7
-
Select a method that suits your needs:
7
+
You can use one of two ways to provide mocked version of `AsyncStorage`:
8
8
9
-
### Mock `node_modules`
9
+
### With __mocks__ directory
10
10
11
11
1. In your project root directory, create `__mocks__/@react-native-community` directory.
12
12
2. Inside that folder, create `async-storage.js` file.
@@ -16,62 +16,72 @@ Select a method that suits your needs:
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, check if it has been called with a specific arguments:
38
+
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:
39
39
40
40
```javascript
41
-
it('checks if Async Storage is used', async () => {
42
-
awaitasyncOperationOnAsyncStorage();
41
+
it('checks if Async Storage is used', async () => {
You can [check mock implementation](../jest/async-storage-mock.js) to get more insight into its signatures.
64
+
You can [check its implementation](../jest/async-storage-mock.js) to get more insight into methods signatures.
65
65
66
66
## Troubleshooting
67
67
68
68
### **`SyntaxError: Unexpected token export` in async-storage/lib/index.js**
69
69
70
-
This is likely because `Jest` is not transforming Async Storage. You can point it to do so, by adding `transformIgnorePatterns` setting in Jest's configuration.
70
+
**Note:** In React Native 0.60+, all `@react-native-community` packages are transformed by default.
71
+
72
+
You need to point Jest to transform this package. You can do so, by adding Async Storage path to `transformIgnorePatterns` setting in Jest's configuration.
0 commit comments