react-native-localize: How to resolve a NativeModule.RNLocalize is null error in test?

Bug

react-native-localize: NativeModule.RNLocalize is null during a jest test run that imports react-native-localize package in the component under test.

image

Things I’ve tried to resolve test error:

  1. I’ve already linked the library via react-native link react-native-localize. 2.I have tried mocking the module in test via jest.mock('react-native-localize');.
  2. I’ve included the suggested mock from the repo README in mocks. see: https://github.com/BrianJVarley/react-native-prototyping/tree/dev/_mocks_

Environment info

react-native info output:

$ react-native info
info Fetching system and libraries information...
System:
    OS: Windows 10 10.0.18363
    CPU: (4) x64 Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
    Memory: 2.50 GB / 7.88 GB
  Binaries:
    Node: 10.13.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.13.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK:
      API Levels: 23, 25, 26, 27, 28, 29
      Build Tools: 23.0.1, 25.0.0, 26.0.1, 27.0.3, 28.0.0, 28.0.1, 28.0.2, 28.0.3, 29.0.0
      System Images: android-25 | Intel x86 Atom, android-25 | Intel x86 Atom_64, android-26 | Google Play Intel x86 Atom, android-27 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom_64
      Android NDK: Not Found
  IDEs:
    Android Studio: Version  3.3.0.0 AI-182.5107.16.33.5199772
  Languages:
    Python: 2.7.15 - /c/Users/brian-varley/.windows-build-tools/python27/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.11.0 => 16.11.0
    react-native: 0.62.0 => 0.62.0
  npmGlobalPackages:
    *react-native*: Not Found

Library version: "react-native-localize": "^1.3.4"

Steps To Reproduce

  1. Run npm install the repo here - https://github.com/BrianJVarley/react-native-prototyping/blob/dev/__tests__/App-test.js
  2. Run npm run test

Describe what you expected to happen:

  1. Jest would mock ‘react-native-localize’ and not throw an error react-native-localize: NativeModule.RNLocalize is null.

Reproducible sample code

Link to test in my repo - https://github.com/BrianJVarley/react-native-prototyping/blob/dev/__tests__/App-test.js

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

The issue still remains, though it is not about testing. After running react-native link react-native-localize also there is no difference.

@BrianJVarley The documentation is here : https://github.com/react-native-community/react-native-localize/blob/master/README.md#how-to-test-your-code

Also, your folder should be named __mocks__, not _mocks_.

Yeah, this is happening to me when I’m importing my own npm package which uses react-native-localize as a dependency.

This issue still exists, when using react-native-localize as a dependency in a project that is a local dependency in another project.

For anyone else who got this while trying to use it in react-native-web you have to remember this part in webpack.config.js

module.exports = {
  resolve: {
    extensions: ['.web.js', '.js'],
  },
};

Makes sense. Thank you @zoontek! I imported it with import RNLocalize from "react-native-localize". Sorry for the confusion. Have a great day!

It will depend of what import style you’re using:

import { getLocales } from "react-native-localize"

// getLocales()
// can to be mocked with:

export const getLocales = () => [/* … */];
import RNLocalize from "react-native-localize"

// RNLocalize.getLocales()
// can to be mocked with:

export default {
  getLocales: () => [/* … */],
}

Which is totally normal 🙂

  • If you use the default exported object, mock it.
  • If you use exported named functions, mocks them.

I am noticing the same thing as @suryaeightfold – a module I am importing that uses react-native-localize does not use the mock that I have provided for it, whereas the mock works perfectly in other situations.