JEST - [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null
See original GitHub issueYou want to:
While running npm jest
, I was thrown by this error here.
FAIL __tests__/App-test.js
● Test suite failed to run
[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.
To fix this issue try these steps:
• Run `react-native link @react-native-community/async-storage` in the project root.
• Rebuild and restart the app.
• Run the packager with `--reset-cache` flag.
• If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then rebuild and re-run the app.
• If this happens while testing with Jest, check out docs how to integrate AsyncStorage with it: https://react-native-community.github.io/async-storage/docs/advanced/jest
If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-async-storage/issues
at Object.<anonymous> (node_modules/@react-native-community/async-storage/lib/commonjs/AsyncStorage.native.js:17:9)
at Object.<anonymous> (node_modules/@react-native-community/async-storage/lib/commonjs/index.js:5:1)
Details:
As of current, I’m not doing any unit test that has anything to do with AsyncStorage. Was simply running the default test given while project creation
/**
* @format
*/
import 'react-native';
import React from 'react';
import App from '../App';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
it('renders correctly', () => {
renderer.create(<App />);
});
However I’ve also tired setting up the mock directory according to the steps given from AsyncStorage lib. Which end up with another error as following:
New Error
FAIL __tests__/App-test.js
● Test suite failed to run
TypeError: Cannot read property 'default' of undefined
1 | import mockAsyncStorage from './__mocks__/@react-native-async-storage/async-storage';
> 2 |
| ^
3 | jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
at jest.setup.js:2:24
at Object.<anonymous> (jest.setup.js:7:44)
Files
/__mocks__/@react-native-async-storage/async-storage.js
export default from '@react-native-async-storage/async-storage/jest/async-storage-mock'
package.json
(only the Jest part is showing)
"jest": {
"preset": "react-native",
"setupFiles": [
"./jest.setup.js"
]
}
jest.setup.js
import mockAsyncStorage from './__mocks__/@react-native-async-storage/async-storage';
jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
NativeModule: AsyncStorage is null. when running jest #202
Current behavior I have two tests running with jest on react native code. One is for a component and one is for a...
Read more >jest test fails after installing react-native-async-storage
But now I'm getting a new error. @RNCommunity/AsyncStorage: NativeModule.RCTAsyncStorage is null. I'm pasting the screenshot to show my project ...
Read more >NativeModule: AsyncStorage is null when running jest - Reddit
I get this AsyncStorage is null when I'm writing tests. I have followed this link by adding mocks dir. https://react-native-async-storage.github ...
Read more >ERROR Error: [@RNC/AsyncStorage]: NativeModule - Medium
ERROR Error: [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null. Rebuild and restart the app. Run the packager with '--reset-cache' flag.
Read more >Troubleshooting | Async Storage - GitHub Pages
This error means that AsyncStorage was unable to find its native module. This occurs because AsyncStorage was not linked into the final app...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi everyone,
I have the same issue where even if I follow the doc, I still get errors for the two ways.
I am only trying to run the default RN test:
Details for Jest setup file
Error:
package.json
tests/jest.setup.js
Details for direct mock
Error:
__mocks__/@react-native-async-storage/async-storage.js
App.test.tsx
Seems like it might be a jest issue.
Our current setup: “@react-native-async-storage/async-storage”: “^1.15.2”, “jest”: “^26.6.3”