0

I am trying to include tests in my App created with Expo, but I am facing errors with AsyncStorage module

Could not find module '@react-native-async-storage/async-storage' from 'src/pages/Welcome.tsx'

I tested some configurations of the jest/expo to avoid this error, as downgrade the jest to version 26 as suggested in a GitHub issue of the project, using a mock directory as suggested here and in the official documentation here, and using jest setup file. Details below.

When I try with mock directory nothing changes in the error. The execution ignores the mock directory. When using the jest setup file the only change in the error message is that the message point to the jest setup file instead of the welcome.tsx.

  • The page tested is the "Welcome.tsx" that import the AsyncStorage
  • The test has nothing, only a a console log (code below) and don't use the AsyncStorage

The test code

const {getAllByTestId} = renderer.create(<Welcome />);
console.log(getAllByTestId);

The Welcome.tsx

...
<Image source=... testID="WelcomeImage" />
...

package.json

...
"scripts": { ... "test": "jest" ...},
"jest:" { 
    "preset": "jest-expo", 
    "globals": { "DEV": true
}  
...

Installed versions

  • React: 17.0.1
  • React native: 0.64.3
  • React native testing library: 6.0.0
  • React test renderer: 17
  • Jest: 27.4.5
Rodrigo Vitor
  • 11
  • 1
  • 6

1 Answers1

0

Follow the directions at - https://react-native-async-storage.github.io/async-storage/docs/advanced/jest/ Here is what I did:

  1. Setup jestSetupFile.js as mentioned.
  2. Setup mocks/@react-native-async-storage directory as mentioned (I know it mentions to use either of two but I did both)
  3. Cleared npm cache, deleted node-modules folder, deleted package-lock.json and did npm i again.(The usual steps to a clean start)

And it works now. Although I get error for enzyme but that is not within the scope of this question.

Appy
  • 11
  • 1