I have replicated an example to understand the testing of Mock-async-storage for reactjs. Any suggestions for a different approach of testing are welcome. I tried to replicate the use case from this stack overflow page : How to test Async Storage with Jest? but I couldn't figure out how that would fit for my sample case. So I tried the below npm module https://github.com/devmetal/mock-async-storage, but that didn't help either.
Code written in Example.test.js
import AsyncStorage from '@react-native-community/async-storage';
beforeEach(() => {
AsyncStorage.clear();
// console.log(`After the data is being reset :`)
// console.log(AsyncStorage)
});
it('can read asyncstorage', async () => {
await AsyncStorage.setItem('username', 'testUser')
let username = await AsyncStorage.getItem('username')
// console.log(`After the data is being set :`)
// console.log(AsyncStorage)
expect(username).toBe('testUser')
});
Code in jest.config.js file :
module.exports = {
setupFilesAfterEnv: [
'./setup-tests.js',
],
};
Code in setup-tests.js
import MockAsyncStorage from 'mock-async-storage';
const mockImpl = new MockAsyncStorage();
jest.mock('@react-native-community/async-storage', () => mockImpl);
Created the mocks folder in root directory and then created @react-native-community folder in it. Then created at async-storage.js file with the following code :
export default from '@react-native-community/async-storage/jest/async-storage-mock'
I am using jest-expo for testing.
The output of the above test case is :