-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
π₯ Testing with jest fails with 'Native module RNFBAppModule not found. Re-check module install, linking, configuration, build and install steps.' #8189
Comments
Using jest to test modules that requires native modules will require mocking and setup of course, this is a typical jest use case you have and a typical jest need for that use case A follow-on in this instance should be: well, how do they do it then? That is, how do we do our testing in our repo so it works for us? https://github.com/invertase/react-native-firebase/blob/main/jest.config.js which has react-native-firebase/jest.config.js Line 8 in 3623a50
Which points to https://github.com/invertase/react-native-firebase/blob/main/jest.setup.ts I'd do what we do in that file - or! if there is a better way - please suggest anything better for that file as we use jest too and we welcome PRs. Should work? |
What I would love to have is a mock file I could import and then mock the libary with that mock. This is a common thing that other libraries do, examples: import mockRNCNetInfo from '@react-native-community/netinfo/jest/netinfo-mock.js';
jest.mock('@react-native-community/netinfo', () => mockRNCNetInfo); import mockRNLocalize from 'react-native-localize/mock/index.js';
jest.mock('react-native-localize', () => mockRNLocalize); jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock')
); If the library would provide such a mock that can be used internally plus for library users, then that would be perfect. Note: The failing of jest is new. This didnt use to happen in |
It would be great if we shipped a mock yes. I also maintain netinfo ( π ), so I'm familiar with this. So how did it go when you used that mock file in your jest setup? There is only so much time in a day - if you opened a PR that added that mock to the list of files in package.json so it was shipped with |
Yes, copy pasting |
It mocks react-native yes, because we do module loading. It mocks the minimum to make that "work" (read as: not fail) in a jest environment, then for the rest of react-native, the mock is the actual implementation of react-native What could possibly go wrong ;-) You tested it and it worked for you, that's great news. No reason we couldn't ship it and people could use it if they like |
I am not familiar with jest but my initial thought was that if we mock rn, then does that override other mocks of rn that a user may already have. I am not very familiar with jest. maybe you already know, or I have to test |
You'd have to test that, however if jest is already mocking things then I would assume (dangerous!) that when our jest says "import" for react-native we'll get the mocked one. it may be then that our mocks will collide with other specific mocks, but ours is mocking the minimum we need - not sure what we can do about that. Then the rest of the react native module from import is spread into our mock so it should still apply I'm not sure a different way to do it though, we have a multi-module system and need to reach into react-native and inspect / load etc our native modules. In a jest context that has to be mocked. |
Issue
Testing a react native app with an import from firebase such as
import messaging from '@react-native-firebase/messaging';
causes jest to fail:Project Files
Javascript
Click To Expand
package.json
:firebase.json
for react-native-firebase v6:Irrelevant here. Doesnt affect testing environment
# N/A
iOS
Click To Expand
ios/Podfile
:Irrelevant
# N/A
AppDelegate.m
:Irrelevant
// N/A
Android
Click To Expand
Have you converted to AndroidX?
Irrelevant
android/gradle.settings
jetifier=true
for Android compatibility?jetifier
for react-native compatibility?android/build.gradle
:// N/A
android/app/build.gradle
:// N/A
android/settings.gradle
:// N/A
MainApplication.java
:// N/A
AndroidManifest.xml
:<!-- N/A -->
Environment
Click To Expand
react-native info
output:react-native-firebase
version you're using that has this issue:e.g. 5.4.3
Firebase
module(s) you're using that has the issue:e.g. Instance ID
TypeScript
?Y/N
&VERSION
React Native Firebase
andInvertase
on Twitter for updates on the library.Info I find relevant:
So basically you can reproduce with:
The error is this:
The problem is that this import:
at Object.require (node_modules/@react-native-firebase/app/lib/internal/registry/nativeModule.js:20:1)
is resolving to
app/lib/internal/nativeModule.ios.js
since apparently ios is the default platform in react native preset for jest.Now this file imports from: app/lib/internal/nativeModuleAndroidIos.js
which tries to get the module from
NativeModules
which obv will return undefined in a jest env:react-native-firebase/packages/app/lib/internal/nativeModuleAndroidIos.js
Line 11 in 3623a50
which leads to the throwing of the error:
react-native-firebase/packages/app/lib/internal/RNFBNativeEventEmitter.js
Lines 23 to 28 in 3623a50
The text was updated successfully, but these errors were encountered: