Skip to content

No mock for 'useAsyncStorage' in jest #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rawatnaresh opened this issue Aug 12, 2020 · 2 comments · Fixed by #425
Closed

No mock for 'useAsyncStorage' in jest #417

rawatnaresh opened this issue Aug 12, 2020 · 2 comments · Fixed by #425

Comments

@rawatnaresh
Copy link

I'm using both AsyncStorage and useAsyncStorage in my application but it turns out there is no mock implementation for 'useAsyncStorage` and I'm getting an error

image

is there a way i can fix this without switching my code to the AsyncStorage

@kieran-osgood
Copy link

kieran-osgood commented Aug 14, 2020

I used this (in my jest.setup.js which is included in my jest.config.js via the: setupFiles: ['<rootDir>/jest.setup.js'], to get up and running:

jest.mock('@react-native-community/async-storage', () => ({
  useAsyncStorage: jest.fn(),
}));

When you want to test based on it returning some example data then adjust jest.fn() to jest.mockReturnValue({...data}). Should be enough for you to continue writing your tests :)

@rawatnaresh
Copy link
Author

Thank you for your reply @KieranO547 . I updated your answer like this and it worked.

jest.mock('@react-native-community/async-storage', () => ({
  useAsyncStorage: () => ({
    getItem: jest.fn(),
    setItem: jest.fn(),
  }),
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants