Why are my firebase emulator get-call promises not resolving in jest unit tests

Viewed 111
  • Using react with firestore as my database running as a local emulator.
  • Testing with react-script test which uses jest.

Now when calling firestore set() and get() functions from my react app in the browser everything works. But when unit testing those same calls only the set() promise resolves. The get() promise never resolves ("Exceeded timeout of 5000 ms for a test.[I tested with timeouts > 100000000]) The emulator receives the get() request without any problem.(It gets logged in the Request tab of the emulator)

This is a simple test which isolates my problem.

import { firestore } from '../firebase';

it('Gets from Firestore', async () => {
  try {
    await firestore.collection("users").doc("testUser").set({ name: "Frank" })
    await firestore.collection("users").doc("testUser").get()
    expect(1).toBe(1);
  } catch (err) {
    console.log(err)
  }
})

Firestore initialization (creds not included):

import firebase from "firebase/app";

import 'firebase/firestore';
import 'firebase/auth';

const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: "",
    measurementId: ""
  };
  
firebase.initializeApp(firebaseConfig);
const firestore = firebase.firestore();
if (window.location.hostname === "localhost") {
  firestore.useEmulator("localhost", 8080);
}
export {firestore}
export default firebase;
1 Answers

Within an angular application I had the same timeout error. It seems that the test environment is not setup properly and is missing some polyfills.

I solved the problem by including the following at the top of the *.spec file

import 'core-js';

I also had another issue where the Angular JIT would fail on some included module/component way down in the test stack, I solved this by including this in the same *.spec file

import "@angular/compiler";

Old question, but hope this helps future me.

Related