Jest mock NativeModules in RN 0.61.2 issue
See original GitHub issueRunning tests (jest) on files that use NativeModules are failing on import. Each file that uses NativeModules returns the following error:
Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'DeviceInfo' could not be found. Verify that a module by this name is registered in the native binary.
It appears the way NativeModules are registered may be conflicting with the TurboModuleRegistry.
React Native version: 0.61.2
System:
OS: macOS Mojave 10.14.5
CPU: (20) x64 Intel(R) Xeon(R) W-2150B CPU @ 3.00GHz
Memory: 33.20 GB / 64.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 8.12.0 - /usr/local/bin/node
npm: 6.4.1 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 13.1, DriverKit 19.0, macOS 10.15, tvOS 13.0, watchOS 6.0
Android SDK:
API Levels: 14, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29
Build Tools: 23.0.1, 23.0.3, 24.0.2, 25.0.0, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.1, 27.0.3, 28.0.0, 28.0.2, 28.0.3, 29.0.0
System Images: android-16 | ARM EABI v7a, android-17 | Google APIs ARM EABI v7a, android-18 | Google APIs ARM EABI v7a, android-23 | Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom, android-23 | Google APIs Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom
IDEs:
Android Studio: 3.2 AI-181.5540.7.32.5014246
Xcode: 11.1/11A1027 - /usr/bin/xcodebuild
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.2 => 0.61.2
npmGlobalPackages:
react-native-cli: 2.0.1
Steps To Reproduce
- Create a new project with
react-native init NativeModulesTest
- Follow the tutorial here to add the Toast module: https://facebook.github.io/react-native/docs/native-modules-android.html
- Add call to
ToastExample.show('Awesome', ToastExample.SHORT);
in App.js react-native run-android
to make sure the module is working as expected.npm t
returns:
> jest
FAIL __tests__/App-test.js
● Test suite failed to run
TypeError: Cannot read property 'show' of undefined
1 | import ToastExample from './ToastExample';
2 |
> 3 | ToastExample.show('Awesome', ToastExample.SHORT);
| ^
at Object.show (Toast.js:3:14)
at Object.<anonymous> (App.js:27:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.74s
Ran all test suites.
npm ERR! Test failed. See above for more details.
- Add a mock to App-test.js for NativeModules
jest.mock('../node_modules/react-native/Libraries/BatchedBridge/NativeModules', () => ({
ToastExample: {
show: jest.fn(),
},
}));
- Run
npm t
returns:
> jest
FAIL __tests__/App-test.js
● Test suite failed to run
Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'DeviceInfo' could not be found. Verify that a module by this name is registered in the native binary.
at invariant (node_modules/invariant/invariant.js:40:15)
at Object.getEnforcing (node_modules/react-native/Libraries/TurboModule/TurboModuleRegistry.js:39:3)
at Object.getEnforcing (node_modules/react-native/Libraries/Utilities/NativeDeviceInfo.js:45:48)
at Object.<anonymous> (node_modules/react-native/Libraries/Utilities/Dimensions.js:15:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.69s
Ran all test suites.
npm ERR! Test failed. See above for more details.
Describe what you expected to happen: imports of files using NativeModules pass tests when the native module is properly mocked.
Snack, code example, screenshot, or link to a repository:
// App-test.js
/**
* @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';
jest.mock('../node_modules/react-native/Libraries/BatchedBridge/NativeModules', () => ({
ToastExample: {
show: jest.fn(),
},
}));
it('renders correctly', () => {
renderer.create(<App />);
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:17
- Comments:6
Top Results From Across the Web
How mock the native module in RN - Stack Overflow
The answer addresses the problem with Jest mocking, I can't guarantee that RN will be workable with these mocks, as it may require...
Read more >Mocking React Native 0.61 modules with Jest - altany's blog
The React team recommends two ways for mocking react-native modules: Specify the full path to the module e.g.. jest.mock(" ...
Read more >Mocking RN modules | React Made Native Easy
When Jest reads the above line, GPS would be null and the test cases will fail. Hence to fix this, we need to...
Read more >Testing React Native Apps - Jest
Mock native modules using jest.mock. The Jest preset built into react-native comes with a few default mocks that are applied on a ...
Read more >jest mock cannot find module - You.com | The AI Search ...
The problem. The reason you're getting that error has to do with how various operations are hoisted. Even though in your original code...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The implementation mentioned in https://github.com/facebook/react-native/issues/26579#issuecomment-578409111 is the only thing that worked for me.
@G0retZ Practically, I was able to make it work by mocking the react-native interface inside
tests/__mocks
:You can add any mocked modules in there, as required by your tests, and export them. Also, you can import the methods in your test files and check whether there were called. For example, if you need to check
ToastExample.show()
you can do:Check this blog post for more details, including mocking platform detection.
So, any workaround for this
Invariant Violation
thing?