react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

Jest + react-native-firebase: Invariant Violation: Native module cannot be null.

Open shahidjabbar opened this issue 9 years ago • 16 comments

Hello there, Thanks for the firebase bridge. We are trying to use the library and it works great, apart from the fact that the jest tests are breaking with the following error:

    Invariant Violation: Native module cannot be null.

      at invariant (node_modules/fbjs/lib/invariant.js:44:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
      at Object.<anonymous> (node_modules/react-native-firebase/lib/modules/base.js:10:23)
      at Object.<anonymous> (node_modules/react-native-firebase/lib/modules/auth/index.js:5:11)

Any idea what can be done about it?

shahidjabbar avatar Jun 13 '17 08:06 shahidjabbar

Hi. I'm a user of this library and we got jest working by mocking the library. You can see some patterns we've used with the library and the mock code here: https://github.com/rmrs/react-native-redux-firebase-patterns. The code in the patterns is explained in this post: https://medium.com/rumors/react-native-redux-and-firebase-for-experts-by-dummies-6376becdd5c8

erezrokah avatar Jun 13 '17 08:06 erezrokah

Thank you so much! It worked! And thanks for the explanation too.

shahidjabbar avatar Jun 13 '17 08:06 shahidjabbar

We're currently using this mock internally. I've posted a gist here with some examples: https://gist.github.com/erezrokah/9faf04db358ec4bbcebb55580122120b

The way it works is by creating per path singletons of ref instances, so you have the same ref instance in you test code and tested code. We might consider properly publishing it (with docs and all) if there is high demand for it.

erezrokah avatar Jun 13 '17 08:06 erezrokah

@erezrokah: i tried configure my test same with your example but it's not work. It's show error: _reactNativeFirebase2.default.reset is not a function. How to mock react-native-firebase with react-native-firebase file in your project?

Shinichi52 avatar Jun 16 '17 10:06 Shinichi52

thanks @erezrokah. it's worked

Shinichi52 avatar Jun 16 '17 15:06 Shinichi52

@erezrokah thanks for sharing your mock, I think it would be interesting publishing it. so others can contribute, it's a bit laborious if have a big app. here are some methods I've added to support partially remote configuration and other modules.

export class MockFirebase {
  constructor() {
    this.database = () => {
      if (!this.databaseInstance) {
        this.databaseInstance = new Database()
      }
      return this.databaseInstance
    }
    this.config = () => ({
      enableDeveloperMode: jest.fn(),
      setDefaults: jest.fn(),
      fetch: () => (new Promise(resolve => resolve())),
      activateFetched: jest.fn(),
    })
    this.auth = jest.fn()
    this.messaging = jest.fn()
    this.storage = jest.fn()
  }
}

alfonsodev avatar Oct 24 '17 17:10 alfonsodev

There's also some interesting work at: https://github.com/soumak77/firebase-mock

Some comments on Jest: https://github.com/soumak77/firebase-mock/issues/36

designorant avatar Oct 24 '17 20:10 designorant

Just want to share that I used https://github.com/cartant/firebase-nightlight and I'm also planning to use https://github.com/cartant/firebase-nightlight

I created ___mocks___/react-native-firebase.js


import { Mock } from 'firebase-nightlight';

const mockDatabase = {
  ...
};

const mock = new Mock({
  database: mockDatabase,
  identities: [{
    email: "[email protected]",
    password: "wonderland"
  }]
});

const mockApp = mock.initializeApp({});

This is an early attempt though.

mjmaix avatar Oct 26 '17 17:10 mjmaix

Hi @Shinichi52 I have the same problem as you. I got this error. TypeError: _reactNativeFirebase.firestore.collection is not a function. This is my simple code

import { firestore } from "react-native-firebase";
const collection = firestore.collection("category").doc("sample");

how did you solve it?

ccfiel avatar Jun 15 '18 02:06 ccfiel

I've faced a mocking name conflict problem which is described in the SO question https://stackoverflow.com/questions/45175599/mock-a-namespace-and-a-function-with-same-name-using-jest

In my case the conflict is between firebase.firestore.FieldValue.ServerTimestamp() and firebase.firestore().

Any idea workaround this?

choipd avatar Aug 13 '18 09:08 choipd

@choipd you can do this

const collection = jest.fn(() => {
  return {
    doc: jest.fn(() => {
      return {
        collection: collection,
        update: jest.fn(() => Promise.resolve(true)),
        onSnapshot: jest.fn(() => Promise.resolve(true)),
        get: jest.fn(() => Promise.resolve(true))
      }
    }),
    where: jest.fn(() => {
      return {
        get: jest.fn(() => Promise.resolve(true)),
        onSnapshot: jest.fn(() => Promise.resolve(true)),
      }
    })
  }
});

const Firestore = () => {
  return {
    collection
  }
}

Firestore.FieldValue = {
  serverTimestamp: jest.fn()
}

codenaz avatar Aug 30 '18 15:08 codenaz

Check out https://github.com/wix/detox - won't need to mock. See https://github.com/ueno-llc/react-native-starter for a starter that uses detox and react-native-firebase.

We use detox to test the library. 👍

Salakar avatar Sep 02 '18 03:09 Salakar

I used this solution and got it working by replacing the filename mockFirebase.js with react-native-firebase.js. Apparently the upcoming react-native-firebase v6 will contain some documentation on this.

jenskuhrjorgensen avatar Aug 19 '19 09:08 jenskuhrjorgensen

@jenskuhrjorgensen glad that worked for you, and it is worth noting that if you have a mocks folder and name a file the same as the module name, you get auto-mocking - that's why that's working for you I think? It is an easy mechanism and what I do now for most modules https://jestjs.io/docs/en/manual-mocks.html#mocking-node-modules

mikehardy avatar Aug 19 '19 13:08 mikehardy

I used this solution and got it working by replacing the filename mockFirebase.js with react-native-firebase.js. Apparently the upcoming react-native-firebase v6 will contain some documentation on this.

that never happen jajaja

ccanalesb avatar May 15 '20 19:05 ccanalesb

Helping documentation with PRs is much greater than laughing - check this out for some pointers - it was just updated today: #3035 - the top right of every page has an edit button that will walk you through making a PR. As a user you will have a better idea of where it might go? If you could make a PR with the info from the last comment you would be the hero we all need 😄 🏆

mikehardy avatar May 15 '20 19:05 mikehardy