Skip to content

[Unit Testing] Difficulties with mocking Firebase services in Jest/Angular environment #3668

Done

Description

@luciodamico

When using @angular/fire/compat or @angular/fire to build Angular applications, we encounter significant challenges when trying to perform unit tests with a testing framework like Jest.

The main issue stems from the tight coupling of the Auth service (and others like Storage or Firestore) to the FirebaseApp instance. Functions like getAuth() and initializeApp() are called implicitly or explicitly by the AngularFire component, which leads to the FirebaseError: No Firebase App '[DEFAULT]' has been created error.

The core problem is that we cannot properly mock these functions before the component attempts to access them. Standard mocking techniques (e.g., jest.mock, TestBed.configureTestingModule providers) fail because the functions are either read-only properties or are called in a context where a FirebaseApp instance is expected, but not yet available.

This forces developers to use fragile workarounds to get tests to pass, making the testing process difficult and prone to errors. It also discourages writing comprehensive unit tests for components that depend on Firebase services.

Suggested Improvement:

We would appreciate it if the AngularFire team could provide an official, documented strategy for unit testing with Jest. A potential solution could involve a testing utility library or a recommended pattern that correctly mocks the underlying FirebaseApp instance and its dependent services in a reliable way.

Activity

  1. added
    comp: docs
    type: chore
    version: current (v17+)
    on Dec 1, 2026
  2. added this to the 22.0.0 milestone on Dec 1, 2026
  3. armando-navarro commented on Dec 1, 2026

    @armando-navarro
    Collaborator

    Hi @luciodamico, thanks for writing this up, and sorry it sat for so long.

    I reproduced what you describe on AngularFire 20.0.1, the version that was current when you filed, and on the releases since. The result is a testing guide in #3855. Here is the short version, in case it helps before that merges.

    What was failing

    • jest.spyOn on a function exported from @angular/fire/auth throws Cannot redefine property: authState. Jest throws the same for firebase/auth itself, so my read is that it comes from how Jest compiles module exports and not from AngularFire.
    • No Firebase App '[DEFAULT]' has been created appears in a test when the code under test calls getAuth() itself, or when nothing gives the test an app.
    • On Jest 30 with jsdom, importing firebase/auth fails with fetch is not defined before any test runs.

    What works

    • jest.mock('@angular/fire/auth', () => ({ ... })) with a factory that returns the names your code imports.
    • A small fake object provided for Auth through TestBed, with no module mock.
    • A real app built from made-up options, provideFirebaseApp(() => initializeApp({ apiKey: 'fake-api-key', projectId: 'demo-project' })). Creating it and reading a signed-out authState needs no network.
    • For Jest 30, a small resolver file that makes Jest load the browser build of Firebase Auth.

    The guide has the full examples. I held off on a separate testing utility library, since each working setup turned out to be a few lines, but I may be missing cases from your project. If you have a test that none of this covers, could you post it here? The pull request closes this issue when it merges, but I'm happy to reopen it.

  4. added a commit that references this issue on Dec 1, 2026
    4627bab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp: docstype: choreversion: current (v17+)

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions