3

I have a simple React frontend application that uses Firebase for Auth and Firestore (https://jlowen-netflix.netlify.app/ if additional context helpful). I provide Firebase as a Context to my top-level application, and listen for a change in Auth to redirect the user to logged in content.

I would like to test end-to-end user workflow from anonymous, to sign-up, to logged in. I have already installed the Firebase CLI as well as the Firebase emulators. I can verify the emulators run correctly by visiting the Emulator UI on localhost. I also can verify the application hooks in to the emulators when I start the dev server (npm start) for Create-React-App. I made a test user in the Emulator UI, and was able to log in as expected.

When I try to get this to render in Jest, my auth listener custom hook attempts to access the auth() method on the firebase context prior to firebase initialization.

 TypeError: Cannot read property 'auth' of undefined

       9 |   // As recommended by Firebase - need to set up subscription
      10 |   useEffect(() => {
    > 11 |     const listener = firebase.auth().onAuthStateChanged((authUser) => {
         |                               ^
      12 |       console.log(`Firebase detected auth change: ${authUser}`)
      13 |       if (authUser) {
      14 |         localStorage.setItem("authUser", JSON.stringify(authUser))

      at src/hooks/use-auth-listener.js:11:31

I can't seem to resolve what seems to be some sort of async issue that I encounter in Jest, but does not occur in the deployed application.

I am pretty new in React and Jest, so if I can provide additional information, please let me know!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
JLowe-N
  • 43
  • 3

1 Answers1

0

Unfortunately your error doesn't seem to be related to your main question. What the error you're showing says is that you forgot to import or require the firebase from somewhere. Read the firebase package docs and add something like this at the top of your file:

var firebase = require('firebase');

I would also be interested in connecting firebase app to local emulators, so will edit this answer if I figure something out. In the mean time.. sorry for the false hope.

kub1x
  • 3,272
  • 37
  • 38