Skip to content

Commit b866063

Browse files
author
Krzysztof Borowy
committed
docs: transform info
1 parent 424e77b commit b866063

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

docs/Jest-integration.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Jest integration
22

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

55
## Using Async Storage mock
66

7-
Select a method that suits your needs:
7+
You can use one of two ways to provide mocked version of `AsyncStorage`:
88

9-
### Mock `node_modules`
9+
### With __mocks__ directory
1010

1111
1. In your project root directory, create `__mocks__/@react-native-community` directory.
1212
2. Inside that folder, create `async-storage.js` file.
@@ -16,62 +16,72 @@ Select a method that suits your needs:
1616
export default from '@react-native-community/async-storage/jest/async-storage-mock'
1717
```
1818

19-
### Use Jest setup files
19+
### With Jest setup file
2020

2121
1. In your Jest config (probably in `package.json`) add setup files location:
2222

2323
```json
24-
"jest": {
25-
"setupFiles": ["./path/to/jestSetupFile.js"]
26-
}
24+
"jest": {
25+
"setupFiles": ["./path/to/jestSetupFile.js"]
26+
}
2727
```
2828

2929
2. Inside your setup file, set up Async Storage mocking:
3030

3131
```javascript
32-
import mockAsyncStorage from '@react-native-community/async-storage/jest/async-storage-mock';
32+
import mockAsyncStorage from '@react-native-community/async-storage/jest/async-storage-mock';
3333

34-
jest.mock('@react-native-community/async-storage', () => mockAsyncStorage);
34+
jest.mock('@react-native-community/async-storage', () => mockAsyncStorage);
3535
```
3636
## Testing with mock
3737

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

4040
```javascript
41-
it('checks if Async Storage is used', async () => {
42-
await asyncOperationOnAsyncStorage();
41+
it('checks if Async Storage is used', async () => {
42+
await asyncOperationOnAsyncStorage();
4343

44-
expect(AsyncStorage.getItem).toBeCalledWith('myKey');
45-
})
44+
expect(AsyncStorage.getItem).toBeCalledWith('myKey');
45+
})
4646
```
4747

4848
## Overriding Mock logic
4949

50-
You can override Async Storage mock implementation, by replacing its inner functions:
50+
You can override mock implementation, by replacing its inner functions:
5151

5252
```javascript
5353
// somewhere in your configuration files
5454
import AsyncStorageMock from '@react-native-community/async-storage/jest/async-storage-mock';
5555

5656
AsyncStorageMock.multiGet = jest.fn(([keys], callback) => {
57-
// do something here to retrieve files
57+
// do something here to retrieve data
5858
callback([]);
5959
})
6060

6161
export default AsyncStorageMock;
6262
```
6363

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

6666
## Troubleshooting
6767

6868
### **`SyntaxError: Unexpected token export` in async-storage/lib/index.js**
6969

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

7274

7375
```json
7476
"jest": {
75-
"transformIgnorePatterns": ["/node_modules/@react-native-community/async-storage/(?!(lib))"]
76-
}
77+
"transformIgnorePatterns": ["node_modules/(?!(@react-native-community/async-storage/lib))"]
78+
}
7779
```
80+
81+
Optionally, you can transform whole scope for `react-native-community` and `react-native`:
82+
83+
```json
84+
"jest": {
85+
"transformIgnorePatterns": ["node_modules/(?!(@react-native-community|react-native))"]
86+
}
87+
```

0 commit comments

Comments
 (0)