Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

javascript - Can Firebase Emulators be used for integration testing with a React frontend?

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...