Firebase-js-sdk: App works fine, but is not testable once include firestore.

Created on 11 Nov 2017  ยท  6Comments  ยท  Source: firebase/firebase-js-sdk

[REQUIRED] Describe your environment

  • Operating System version: macOS 10.13 / node v6.10.3
  • Firebase SDK version: "firebase": "^4.6.2",
  • Firebase Product: firestore

[REQUIRED] Describe the problem

Building my react/redux/flowtype app, writing tests, all is well. As soon as I bring it firestore into one of my redux action creators, the most basic app test now fails. (But the app works as expected). The test suite (code below) fails to initialize with the folowing error:

โ— Test suite failed to run

Cannot find module 'grpc' from 'grpc_connection.js'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
  at Object.<anonymous> (node_modules/@firebase/firestore/dist/cjs/src/platform_node/grpc_connection.js:29:12)

Steps to reproduce:

? Include a jest test against app that includes firestore downstream. If I comment out from my app

// require('firebase/firestore');
// const db = Firebase.firestore();

then all tests pass again (but app breaks, of course.)

Relevant Code:

import React from 'react';
import App from '../App';


describe('Main App component', () => {
  it('matches snapshot', () => {
    // as soon as I include firestore anywhere in my app I get this error:
    // Cannot find module 'grpc' from 'grpc_connection.js' 
    // and test fails
    const wrapper = shallow(
      <App />
    );
    expect(wrapper).toMatchSnapshot();
  })

  it('above is broken, firestore issue, see notes', () => {
    expect(true).toEqual(true)
  })
})
firestore

Most helpful comment

@jakelowen What are you using to test? If it is jest, see the explainer below. If not, could you provide me an example repo so I can see what is going on?

@mathieuseguin in the repro you sent you are using jest with the --env=jsdom flag, but without the --browser flag. They are actually both required to properly test browser code.

Firebase specifies a pkg.browser so providing Jest's browser flag is required.

For your demo, simply adding a --browser to the end of the test script alleviated the issue for me.

All 6 comments

I'm having the exact same issue with Jest

Here is a test app to reproduce:

https://github.com/mathieuseguin/firestore-jest-bug

any update on this? I am also facing the same issue on firebase": "^4.5.2",

@jakelowen What are you using to test? If it is jest, see the explainer below. If not, could you provide me an example repo so I can see what is going on?

@mathieuseguin in the repro you sent you are using jest with the --env=jsdom flag, but without the --browser flag. They are actually both required to properly test browser code.

Firebase specifies a pkg.browser so providing Jest's browser flag is required.

For your demo, simply adding a --browser to the end of the test script alleviated the issue for me.

Amazing! Thanks @jshcrowthe!

It does resolve the issue for me.

That fixed it for me as well. Thanks @jshcrowthe!

Was this page helpful?
0 / 5 - 0 ratings