[Jest] Mocked async storage
See original GitHub issueCurrent behavior
Hey guys !
I tried to mock async storage by applying what is written in the “jest integration” section.
i’m getting Cannot read property 'getItem' of undefined
when running tests. The app works without any problem when launched, the issue only appears when running tests.
I am not testing directly async storage in my tests, I am testing a component that uses AsyncStorage.
The test:
it('LoginDeleteAccount renders correctly', () => {
const { toJSON } = render(<MockedNavigator component={LoginDeleteAccount} />)
expect(toJSON()).toMatchSnapshot()
})
Inside the component LoginDeleteAccount
:
async retrieveEmails() {
try {
const emails_array = await AsyncStorage.getItem('emails')
...
} catch (error) {
console.log(error)
}
}
As for the mocking, I have a index.js
file in __mocks__/@react-native-community/async-storage
.
In it:
export default from '@react-native-community/async-storage/jest/async-storage-mock'
Any ideas ? thanks in advance !
Expected behavior
Test runs without any errors
Repro steps
- npm test
Environment
- Async Storage version: 1.11.0
- React-Native version: 0.62.2
- Platform tested: Jest(terminal)/iOS / Android
- Logs/Error that are relevant:
error:
TypeError: Cannot read property 'getItem' of undefined
Issue Analytics
- State:
- Created 3 years ago
- Comments:22 (5 by maintainers)
Top Results From Across the Web
Jest integration | Async Storage - GitHub Pages
Jest integration. Async Storage module is tightly coupled with its NativeModule part - it needs a running React Native application to work properly....
Read more >How to test Async Storage with Jest?
Inside that file, export Async Storage mock. ... Jest should then mock AsyncStorage by default in all your tests. If it doesn't, try...
Read more >mock-async-storage
Its a mock of react-native AsyncStorage for jest tests. Latest version: 2.2.0, last published: 3 years ago. Start using mock-async-storage ...
Read more >Jest integration - invertase/react-native-async-storage
Using Async Storage mock. You can use one of two ways to provide mocked version of AsyncStorage : With mocks directory. In ...
Read more >mock-async-storage
Jest Mock AsyncStorage for react-native. Travis CI Build Status. Standard - JavaScript Style Guide. Its a mock of react-native AsyncStorage for jest tests ......
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
I temporarily solved
cannot read .catch of undefined
when usingredux-persist
.Instead of using the ready-made mocks, I manually added this to my
jest.setup.ts
Or, better. Copy and paste this to your project’s
__mocks__
(create this directory if it does not exist)__mocks__/@react-native-community/async-storage/index.js
I don’t know why wrapping the async functions with
jest.fn(async () => {})
does not work.I think I am having the same issue when using AsyncStorage through redux-persist.