Skip to content

Error running tests: You attempted to use a firebase module that's not installed natively on your iOS project by calling firebase.app() #3633

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

Closed
2 of 10 tasks
casconed opened this issue May 8, 2020 · 7 comments

Comments

@casconed
Copy link

casconed commented May 8, 2020

Issue

Installed @react-native-firebase/messaging @6.3.4 (to match other deps in the project) and getting the following output trying to run tests using jest

 You attempted to use a firebase module that's not installed natively on your iOS project by calling firebase.app().
    Ensure you have either linked the module or added it to your projects Podfile.
    See http://invertase.link/ios for full setup instructions.
      29 |     requestPermission()
      30 | 
    > 31 |     return messaging().onMessage(async remoteMessage => {
         |            ^
      32 |       console.log('FCM Message Data:', remoteMessage)
      33 | 
      34 |       // Update a users messages list using AsyncStorage
      at getAppModule (node_modules/@react-native-firebase/app/lib/internal/registry/nativeModule.js:227:11)
      at initializeNativeApps (node_modules/@react-native-firebase/app/lib/internal/registry/app.js:49:24)
      at getApp (node_modules/@react-native-firebase/app/lib/internal/registry/app.js:79:5)
      at firebaseModuleWithApp (node_modules/@react-native-firebase/app/lib/internal/registry/namespace.js:138:25)
      at App.js:31:12
      at commitHookEffectList (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12131:26)
      at commitPassiveHookEffects (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12165:11)
      at Object.invokeGuardedCallbackImpl (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11669:10)
      at invokeGuardedCallback (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11846:31)
      at flushPassiveEffectsImpl (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15379:7)

Project Files

Javascript

Click To Expand

package.json:

{
  "scripts": {
    "start": "react-native start",
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "web": "expo start --web",
    "test": "standard && jest --watchAll",
    "travis": "standard && jest --forceExit"
  },
  "jest": {
    "preset": "react-native",
    "setupFiles": [
      "<rootDir>/scripts/jest/setup.js"
    ],
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native|react-navigation|expo-web-browser|@unimodules|@react-native-firebase|@react-navigation|@react-native-community))"
    ]
  },
  "dependencies": {
    "@expo/samples": "~36.0.0",
    "@expo/vector-icons": "~10.0.0",
    "@react-native-community/async-storage": "^1.7.1",
    "@react-native-community/blur": "^3.6.0",
    "@react-native-community/datetimepicker": "^2.3.2",
    "@react-native-community/netinfo": "^5.7.1",
    "@react-native-firebase/app": "^6.3.4",
    "@react-native-firebase/auth": "^6.3.4",
    "@react-native-firebase/crashlytics": "^6.3.4",
    "@react-native-firebase/firestore": "^6.3.4",
    "@react-native-firebase/functions": "^6.3.4",
    "@react-native-firebase/messaging": "^6.3.4",
    "@react-native-firebase/remote-config": "^6.3.4",
    "@react-navigation/web": "~1.0.0-alpha.9",
    "expo": "~36.0.0",
    "expo-asset": "~8.0.0",
    "expo-constants": "~8.0.0",
    "expo-font": "~8.0.0",
    "expo-web-browser": "^8.0.0",
    "libphonenumber-js": "^1.7.31",
    "metro-config": "latest",
    "moment": "^2.24.0",
    "react": "^16.11.0",
    "react-dom": "~16.9.0",
    "react-native": "^0.62.1",
    "react-native-config": "^0.12.0",
    "react-native-default-preference": "^1.4.2",
    "react-native-gesture-handler": "^1.5.6",
    "react-native-reanimated": "^1.4.0",
    "react-native-screens": "2.0.0-alpha.12",
    "react-native-svg": "^10.0.0",
    "react-native-svg-transformer": "^0.14.3",
    "react-native-swipe-gestures": "^1.0.4",
    "react-native-touch-id": "^4.4.1",
    "react-native-ui-lib": "^5.6.1",
    "react-native-unimodules": "~0.7.0",
    "react-navigation": "~4.0.10",
    "react-navigation-stack": "~1.10.3"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-transform-destructuring": "^7.8.3",
    "@babel/plugin-transform-flow-strip-types": "^7.8.3",
    "@babel/preset-env": "^7.8.3",
    "@babel/preset-flow": "^7.8.3",
    "@babel/preset-react": "^7.8.3",
    "babel-jest": "^24.9.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-expo": "~8.0.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-react-native": "^4.0.1",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2",
    "eslint-config-standard": "^14.1.1",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.1",
    "husky": "^4.2.5",
    "jest-expo": "~36.0.1",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-native-babel-jest": "^0.2.1",
    "react-test-renderer": "^16.11.0",
    "standard": "^14.3.1"
  },
  "husky": {
    "hooks": {
      "pre-commit": "standard --fix"
    }
  },
  "standard": {
    "env": [
      "jest"
    ],
    "options": {
      "enable": true,
      "parser": "babel-eslint"
    }
  },
  "private": true
}

firebase.json for react-native-firebase v6:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  }
}

iOS

Click To Expand

ios/Podfile:

  • I'm not using Pods
  • I'm using Pods and my Podfile looks like:
platform :ios, '10.0'

require_relative '../node_modules/react-native-unimodules/cocoapods'


def add_flipper_pods!
  version = '~> 0.33.1'
  pod 'FlipperKit', version, :configuration => 'Debug'
  pod 'FlipperKit/FlipperKitLayoutPlugin', version, :configuration => 'Debug'
  pod 'FlipperKit/SKIOSNetworkPlugin', version, :configuration => 'Debug'
  pod 'FlipperKit/FlipperKitUserDefaultsPlugin', version, :configuration => 'Debug'
  pod 'FlipperKit/FlipperKitReactPlugin', version, :configuration => 'Debug'
end
# Post Install processing for Flipper
def flipper_post_install(installer)
  installer.pods_project.targets.each do |target|
    if target.name == 'YogaKit'
      target.build_configurations.each do |config|
        config.build_settings['SWIFT_VERSION'] = '4.1'
      end
    end
  end
end

target 'Sparkles' do

  rnPrefix = "../node_modules/react-native"

  # React Native and its dependencies
  pod 'FBLazyVector', :path => "#{rnPrefix}/Libraries/FBLazyVector"
  pod 'FBReactNativeSpec', :path => "#{rnPrefix}/Libraries/FBReactNativeSpec"
  pod 'RCTRequired', :path => "#{rnPrefix}/Libraries/RCTRequired"
  pod 'RCTTypeSafety', :path => "#{rnPrefix}/Libraries/TypeSafety"
  pod 'React', :path => "#{rnPrefix}/"
  pod 'React-Core', :path => "#{rnPrefix}/"
  pod 'React-CoreModules', :path => "#{rnPrefix}/React/CoreModules"
  pod 'React-RCTActionSheet', :path => "#{rnPrefix}/Libraries/ActionSheetIOS"
  pod 'React-RCTAnimation', :path => "#{rnPrefix}/Libraries/NativeAnimation"
  pod 'React-RCTBlob', :path => "#{rnPrefix}/Libraries/Blob"
  pod 'React-RCTImage', :path => "#{rnPrefix}/Libraries/Image"
  pod 'React-RCTLinking', :path => "#{rnPrefix}/Libraries/LinkingIOS"
  pod 'React-RCTNetwork', :path => "#{rnPrefix}/Libraries/Network"
  pod 'React-RCTSettings', :path => "#{rnPrefix}/Libraries/Settings"
  pod 'React-RCTText', :path => "#{rnPrefix}/Libraries/Text"
  pod 'React-RCTVibration', :path => "#{rnPrefix}/Libraries/Vibration"
  pod 'React-Core/RCTWebSocket', :path => "#{rnPrefix}/"
  pod 'React-Core/DevSupport', :path => "#{rnPrefix}/"
  pod 'React-cxxreact', :path => "#{rnPrefix}/ReactCommon/cxxreact"
  pod 'React-jsi', :path => "#{rnPrefix}/ReactCommon/jsi"
  pod 'React-jsiexecutor', :path => "#{rnPrefix}/ReactCommon/jsiexecutor"
  pod 'React-jsinspector', :path => "#{rnPrefix}/ReactCommon/jsinspector"
  pod 'ReactCommon/callinvoker', :path => "#{rnPrefix}/ReactCommon"
  pod 'ReactCommon/turbomodule/core', :path => "#{rnPrefix}/ReactCommon"
  pod 'Yoga', :path => "#{rnPrefix}/ReactCommon/yoga", :modular_headers => true
  pod 'DoubleConversion', :podspec => "#{rnPrefix}/third-party-podspecs/DoubleConversion.podspec"
  pod 'glog', :podspec => "#{rnPrefix}/third-party-podspecs/glog.podspec"
  pod 'Folly', :podspec => "#{rnPrefix}/third-party-podspecs/Folly.podspec"

  # Other native modules
  pod 'RNGestureHandler', :podspec => '../node_modules/react-native-gesture-handler/RNGestureHandler.podspec'
  pod 'RNReanimated', :podspec => '../node_modules/react-native-reanimated/RNReanimated.podspec'
  pod 'RNScreens', :path => '../node_modules/react-native-screens'

  # Automatically detect installed unimodules
  use_unimodules!
  pod 'react-native-config', :path => '../node_modules/react-native-config'

  pod 'RNFBApp', :path => '../node_modules/@react-native-firebase/app'

  pod 'RNSVG', :path => '../node_modules/react-native-svg'

  pod 'RNCAsyncStorage', :path => '../node_modules/@react-native-community/async-storage'

  pod 'RNFBAuth', :path => '../node_modules/@react-native-firebase/auth'

  pod 'RNFBFirestore', :path => '../node_modules/@react-native-firebase/firestore'

  pod 'RNFBCrashlytics', :path => '../node_modules/@react-native-firebase/crashlytics'

  pod 'RNFBRemoteConfig', :path => '../node_modules/@react-native-firebase/remote-config'

  pod 'RNFBFunctions', :path => '../node_modules/@react-native-firebase/functions'

  pod 'TouchID', :path => '../node_modules/react-native-touch-id'

  pod 'RNDefaultPreference', :path => '../node_modules/react-native-default-preference'

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable these next few lines.
  add_flipper_pods!
  pod 'react-native-blur', :path => '../node_modules/@react-native-community/blur'

  pod 'RNDateTimePicker', :path => '../node_modules/@react-native-community/datetimepicker'

  pod 'react-native-netinfo', :path => '../node_modules/@react-native-community/netinfo'

  pod 'ReactNativeUiLib', :path => '../node_modules/react-native-ui-lib/lib'

  pod 'RNFBMessaging', :path => '../node_modules/@react-native-firebase/messaging'
  pod 'Firebase/Messaging', '~> 6.13.0'

  post_install do |installer|
    flipper_post_install(installer)
  end

end

AppDelegate.m:

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

#import <UMCore/UMModuleRegistry.h>
#import <UMReactNativeAdapter/UMNativeModulesProxy.h>
#import <UMReactNativeAdapter/UMModuleRegistryAdapter.h>

#if DEBUG
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>
static void InitializeFlipper(UIApplication *application) {
  FlipperClient *client = [FlipperClient sharedClient];
  SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
  [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
  [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
  [client addPlugin:[FlipperKitReactPlugin new]];
  [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
  [client start];
}
#endif

// Manually import Firebase and init
@import Firebase;

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  #if DEBUG
    InitializeFlipper(application);
  #endif

  if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
  }
  self.moduleRegistryAdapter = [[UMModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[UMModuleRegistryProvider alloc] init]];
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"Sparkles" initialProperties:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];

  [super application:application didFinishLaunchingWithOptions:launchOptions];

  return YES;
}

- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
{
  NSArray<id<RCTBridgeModule>> *extraModules = [_moduleRegistryAdapter extraModulesForBridge:bridge];
  // You can inject any extra modules that you would like here, more information at:
  // https://facebook.github.io/react-native/docs/native-modules-ios.html#dependency-injection
  return extraModules;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
#ifdef DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end


Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package 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:

System:
    OS: macOS 10.15.4
    CPU: (4) x64 Intel(R) Core(TM) i5-8210Y CPU @ 1.60GHz
    Memory: 437.34 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.16.3 - /usr/local/opt/node@10/bin/node
    Yarn: Not Found
    npm: 6.9.0 - /usr/local/opt/node@10/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.8.4 - /usr/local/lib/ruby/gems/2.6.0/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.4, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 27, 28, 29
      Build Tools: 27.0.0, 28.0.3, 29.0.3
      System Images: android-27 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom_64
      Android NDK: Not Found
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.6010548
    Xcode: 11.4.1/11E503a - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_222 - /usr/bin/javac
    Python: 3.7.6 - /usr/local/opt/python/libexec/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: ^16.11.0 => 16.11.0 
    react-native: ^0.62.1 => 0.62.1 
  npmGlobalPackages:
    *react-native*: Not Found
  • Platform that you're experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you're using that has this issue:
    • 6.3.4
  • Firebase module(s) you're using that has the issue:
    • Messaging
  • Are you using TypeScript?
    • N


@mikehardy
Copy link
Collaborator

If you don't have the results of #3339 which I believe are in 6.7.1 I wouldn't want to use messaging (and certainly we can't support it since all the fixes are in that PR), so I'd look to untangle your deps so you can upgrade.
Either way, this is a general problem with native modules and jest. You have to ignore native modules with jest or mock them out. I think there are some issues related to jest where others have shared strategies, have you checked?

@casconed
Copy link
Author

casconed commented May 8, 2020

@mikehardy realizing that we were pretty far behind, I went ahead and upgraded. Still seeing the same issue. I'll look into the native modules/jest track - it hadn't occurred to me that would be the issue!

@casconed
Copy link
Author

casconed commented May 8, 2020

@mikehardy thanks for the tip. Mocks were the ticket! Leaving these here in case anyone else stumbles into this:

jest.mock('@react-native-firebase/messaging', () => {
  return () => ({
    hasPermission: jest.fn(() => Promise.resolve(true)),
    subscribeToTopic: jest.fn(),
    unsubscribeFromTopic: jest.fn(),
    requestPermission: jest.fn(() => Promise.resolve(true)),
    getToken: jest.fn(() => Promise.resolve('myMockToken')),
    onMessage: jest.fn(),
    onNotificationOpenedApp: jest.fn(),
    getInitialNotification: jest.fn(() => Promise.resolve(false))
  })
})

jest.mock('@react-native-firebase/app', () => {
  return () => ({
    onNotification: jest.fn(),
    onNotificationDisplayed: jest.fn()
  })
})

@casconed casconed closed this as completed May 8, 2020
@mikehardy
Copy link
Collaborator

Nice! Glad that worked

@Nykollas
Copy link

Same error here without using jest!

@mikehardy
Copy link
Collaborator

Then it is not at all the same problem. You have improperly integrayed the module and should carefully examine the setup instructions

@Nykollas
Copy link

I reinstalled the app on emulator, and it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants