question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JEST - [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null

See original GitHub issue

You 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BenjaminSAILcloudcommented, Apr 27, 2021

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:

/**
 * @format
 */

import 'react-native';

import App from '../src/index';
import React from 'react';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  renderer.create(<App />);
});
Details for Jest setup file

Error:

 FAIL  __tests__/App.test.tsx
  ● Test suite failed to run

    [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.

    To fix this issue try these steps:

      • Run `react-native link @react-native-async-storage/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-async-storage.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-async-storage/react-native-async-storage/issues

      at Object.<anonymous> (node_modules/@react-native-async-storage/async-storage/lib/commonjs/AsyncStorage.native.js:17:9)
      at Object.<anonymous> (node_modules/@react-native-async-storage/async-storage/lib/commonjs/index.js:6:1)

package.json

"jest": {
    "setupFiles": [
      "./tests/jest.setup.js"
    ]
  }

tests/jest.setup.js

import {jest} from '@jest/globals'; // tried with and without this line
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';

jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);

Details for direct mock

Error:

 FAIL  __tests__/App.test.tsx
  ● Test suite failed to run

    TypeError: Cannot read property 'default' of undefined

      11 | import renderer from 'react-test-renderer';
      12 |
    > 13 | jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
         |                                                              ^
      14 |
      15 | it('renders correctly', () => {
      16 |   renderer.create(<App />);

      at __tests__/App.test.tsx:13:62
      at Object.<anonymous> (src/providers/global_provider.js:3:1)
      at Object.<anonymous> (src/index.js:3:1)

__mocks__/@react-native-async-storage/async-storage.js

export default from '@react-native-async-storage/async-storage/jest/async-storage-mock';

App.test.tsx

/**
 * @format
 */

import 'react-native';

import App from '../src/index';
import React from 'react';
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);

it('renders correctly', () => {
  renderer.create(<App />);
});

1reaction
radko93commented, Apr 20, 2021
  1. Using async storage directly in the project codebase works.
  2. Importing a node module that uses async storage under the hood does not work.

Seems like it might be a jest issue.

Our current setup: “@react-native-async-storage/async-storage”: “^1.15.2”, “jest”: “^26.6.3”

import { jest } from '@jest/globals';
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';

jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
"jest": {
    "setupFiles": [
      "./jestSetupFile.js"
    ],
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "globals": {
      "window": {}
    }
  }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found