Skip to content

[docs] Mocking of angularfire methods with angularfire 7 during tests #3050

In review

Description

@alexis-mrc

Version info

Angular: 12

Firebase: 9

AngularFire: 7

How to reproduce these conditions

How to mock firebase operator during tests suits ?
How to write unit tests with mocks of firestore/auth/analytics methods ?

Expected behavior

With Angularfire 6, I was able to mock angularfire methods like doc, collection and other methods to write my unit tests.
I would like to be able to mock it.

Actual behavior

I am getting the error during my unit tests :
Error: Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported).

Activity

  1. google-oss-bot commented on Nov 8, 2021

    @google-oss-bot

    This issue does not seem to follow the issue template. Make sure you provide all the required information.

  2. kekel87 commented on Jul 26, 2022

    @kekel87

    So, after a year with no response, I'm sharing my workaround.

    For the context, jasmine is not able to spy the functions exported by Typescript 4.

    (This is possible with jest for those who use it).

    For those who use jasmine, the famous wrapper technique 🤮, allows to spy and mock the functions:

    import { authState, signInWithEmailAndPassword, signInWithPopup, signOut } from '@angular/fire/auth';
    
    export abstract class AngularFireShimWrapper {
      static readonly authState = authState;
      static readonly signInWithEmailAndPassword = signInWithEmailAndPassword;
      static readonly signInWithPopup = signInWithPopup;
      static readonly signOut = signOut;
    }

    AuthService to test :

    import { Injectable } from '@angular/core';
    import { Auth, GoogleAuthProvider } from '@angular/fire/auth';
    import { Observable, from } from 'rxjs';
    import { map } from 'rxjs/operators';
    
    import { AngularFireShimWrapper } from '~shared/utils/angular-fire-shim-wrapper';
    
    import { User } from './user.model';
    
    @Injectable({
      providedIn: 'root',
    })
    export class AuthService {
      user$: Observable<User | null>;
    
      constructor(private auth: Auth) {
        this.user$ = AngularFireShimWrapper.authState(this.auth).pipe(
          map((user) =>
            user !== null
              ? ({
                  uid: user.uid,
                  displayName: user.displayName,
                  photoURL: user.photoURL,
                  email: user.email,
                } as User)
              : null
          )
        );
      }
    
      signOut(): Observable<void> {
        return from(AngularFireShimWrapper.signOut(this.auth));
      }
    
      signInWithGoogle() {
        return from(AngularFireShimWrapper.signInWithPopup(this.auth, new GoogleAuthProvider()));
      }
    
      signInWithEmailAndPassword(email: string, password: string) {
        return from(AngularFireShimWrapper.signInWithEmailAndPassword(this.auth, email, password));
      }
    }

    Units tests :

    import { Auth, User, UserCredential } from '@angular/fire/auth';
    import { MockBuilder, ngMocks } from 'ng-mocks';
    import { BehaviorSubject } from 'rxjs';
    import { first } from 'rxjs/operators';
    
    import { AngularFireShimWrapper } from '~shared/utils/angular-fire-shim-wrapper';
    import { mockUser } from '~tests/mocks/user.spec';
    
    import { AuthService } from './auth.service';
    
    fdescribe('AuthService', () => {
      let authService: AuthService;
      const auth = jasmine.createSpy();
      const authState$ = new BehaviorSubject<User | null>(null);
    
      beforeEach(async () => {
        spyOn(AngularFireShimWrapper, 'authState').and.returnValue(authState$);
    
        await MockBuilder(AuthService).provide({ provide: Auth, useValue: auth });
    
        authService = ngMocks.findInstance(AuthService);
      });
    
      it('should be created', () => {
        expect(authService).toBeDefined();
      });
    
      it('should handle null User', (done: DoneFn) => {
        authState$.next(null);
    
        authService.user$.pipe(first()).subscribe({
          next: (v) => {
            expect(v).toBeNull();
            done();
          },
          error: done.fail,
        });
      });
    
      it('should return an observable of the user', (done: DoneFn) => {
        authState$.next(mockUser as unknown as User);
    
        authService.user$.pipe(first()).subscribe({
          next: (v) => {
            expect(v).toEqual(mockUser);
            done();
          },
          error: done.fail,
        });
      });
    
      it('should call the popup opening method', (done: DoneFn) => {
        spyOn(AngularFireShimWrapper, 'signInWithPopup').and.returnValue(Promise.resolve({} as unknown as UserCredential));
    
        authService
          .signInWithGoogle()
          .pipe(first())
          .subscribe({
            next: () => {
              expect(AngularFireShimWrapper.signInWithPopup).toHaveBeenCalled();
              done();
            },
            error: done.fail,
          });
      });
    
      it('should call the popup opening method', (done: DoneFn) => {
        spyOn(AngularFireShimWrapper, 'signInWithEmailAndPassword').and.returnValue(Promise.resolve({} as unknown as UserCredential));
    
        authService
          .signInWithEmailAndPassword('test@test.fr', '123456')
          .pipe(first())
          .subscribe({
            next: () => {
              expect(AngularFireShimWrapper.signInWithEmailAndPassword).toHaveBeenCalledWith(auth, 'test@test.fr', '123456');
              done();
            },
            error: done.fail,
          });
      });
    
      it('should call the sign method', () => {
        spyOn(AngularFireShimWrapper, 'signOut').and.returnValue(Promise.resolve());
    
        authService.signOut();
    
        expect(AngularFireShimWrapper.signOut).toHaveBeenCalled();
      });
    });

    giphy (1)

    The logic remains the same for all functions exposed directly from version 7 of angular/fire.

  3. docaohuynh commented on Feb 6, 2023

    @docaohuynh

    So, after a year with no response, I'm sharing my workaround.

    For the context, jasmine is not able to spy the functions exported by Typescript 4.

    (This is possible with jest for those who use it).

    For those who use jasmine, the famous wrapper technique 🤮, allows to spy and mock the functions:

    import { authState, signInWithEmailAndPassword, signInWithPopup, signOut } from '@angular/fire/auth';
    
    export abstract class AngularFireShimWrapper {
      static readonly authState = authState;
      static readonly signInWithEmailAndPassword = signInWithEmailAndPassword;
      static readonly signInWithPopup = signInWithPopup;
      static readonly signOut = signOut;
    }

    AuthService to test :

    import { Injectable } from '@angular/core';
    import { Auth, GoogleAuthProvider } from '@angular/fire/auth';
    import { Observable, from } from 'rxjs';
    import { map } from 'rxjs/operators';
    
    import { AngularFireShimWrapper } from '~shared/utils/angular-fire-shim-wrapper';
    
    import { User } from './user.model';
    
    @Injectable({
      providedIn: 'root',
    })
    export class AuthService {
      user$: Observable<User | null>;
    
      constructor(private auth: Auth) {
        this.user$ = AngularFireShimWrapper.authState(this.auth).pipe(
          map((user) =>
            user !== null
              ? ({
                  uid: user.uid,
                  displayName: user.displayName,
                  photoURL: user.photoURL,
                  email: user.email,
                } as User)
              : null
          )
        );
      }
    
      signOut(): Observable<void> {
        return from(AngularFireShimWrapper.signOut(this.auth));
      }
    
      signInWithGoogle() {
        return from(AngularFireShimWrapper.signInWithPopup(this.auth, new GoogleAuthProvider()));
      }
    
      signInWithEmailAndPassword(email: string, password: string) {
        return from(AngularFireShimWrapper.signInWithEmailAndPassword(this.auth, email, password));
      }
    }

    Units tests :

    import { Auth, User, UserCredential } from '@angular/fire/auth';
    import { MockBuilder, ngMocks } from 'ng-mocks';
    import { BehaviorSubject } from 'rxjs';
    import { first } from 'rxjs/operators';
    
    import { AngularFireShimWrapper } from '~shared/utils/angular-fire-shim-wrapper';
    import { mockUser } from '~tests/mocks/user.spec';
    
    import { AuthService } from './auth.service';
    
    fdescribe('AuthService', () => {
      let authService: AuthService;
      const auth = jasmine.createSpy();
      const authState$ = new BehaviorSubject<User | null>(null);
    
      beforeEach(async () => {
        spyOn(AngularFireShimWrapper, 'authState').and.returnValue(authState$);
    
        await MockBuilder(AuthService).provide({ provide: Auth, useValue: auth });
    
        authService = ngMocks.findInstance(AuthService);
      });
    
      it('should be created', () => {
        expect(authService).toBeDefined();
      });
    
      it('should handle null User', (done: DoneFn) => {
        authState$.next(null);
    
        authService.user$.pipe(first()).subscribe({
          next: (v) => {
            expect(v).toBeNull();
            done();
          },
          error: done.fail,
        });
      });
    
      it('should return an observable of the user', (done: DoneFn) => {
        authState$.next(mockUser as unknown as User);
    
        authService.user$.pipe(first()).subscribe({
          next: (v) => {
            expect(v).toEqual(mockUser);
            done();
          },
          error: done.fail,
        });
      });
    
      it('should call the popup opening method', (done: DoneFn) => {
        spyOn(AngularFireShimWrapper, 'signInWithPopup').and.returnValue(Promise.resolve({} as unknown as UserCredential));
    
        authService
          .signInWithGoogle()
          .pipe(first())
          .subscribe({
            next: () => {
              expect(AngularFireShimWrapper.signInWithPopup).toHaveBeenCalled();
              done();
            },
            error: done.fail,
          });
      });
    
      it('should call the popup opening method', (done: DoneFn) => {
        spyOn(AngularFireShimWrapper, 'signInWithEmailAndPassword').and.returnValue(Promise.resolve({} as unknown as UserCredential));
    
        authService
          .signInWithEmailAndPassword('test@test.fr', '123456')
          .pipe(first())
          .subscribe({
            next: () => {
              expect(AngularFireShimWrapper.signInWithEmailAndPassword).toHaveBeenCalledWith(auth, 'test@test.fr', '123456');
              done();
            },
            error: done.fail,
          });
      });
    
      it('should call the sign method', () => {
        spyOn(AngularFireShimWrapper, 'signOut').and.returnValue(Promise.resolve());
    
        authService.signOut();
    
        expect(AngularFireShimWrapper.signOut).toHaveBeenCalled();
      });
    });

    giphy (1) giphy (1)

    The logic remains the same for all functions exposed directly from version 7 of angular/fire.

    Saved my day literally

  4. Kwabena-Agyeman commented on Aug 4, 2023

    @Kwabena-Agyeman

    Dont know how you came up with this, but excellent job!!!

    New to angular and this helped me out A LOT

  5. added this to the 22.0.0 milestone on Dec 1, 2026
  6. added
    comp: docs
    type: chore
    version: legacy (v7-v16)
    on Dec 1, 2026
  7. added a commit that references this issue on Dec 1, 2026
    ad786a0
  8. armando-navarro commented on Dec 1, 2026

    @armando-navarro
    Collaborator

    Hi all, and sorry this went without an answer for so long.

    The testing guide is getting a Karma and Jasmine section in #3856. I measured it on AngularFire 20.1.1, 21.0.0 and 22.0.0-rc.1, so some of it differs from what you saw on AngularFire 7. Here is the short version.

    What I found

    • spyOn on a function exported from @angular/fire/auth still fails, with <spyOn> : authState is not declared writable or has no setter. It fails the same way on firebase/auth itself, so my read is that it comes from how module exports are compiled and not from AngularFire.
    • The error in the original report, Either AngularFireModule has not been provided in your AppModule, is no longer in AngularFire.

    What works under Jasmine

    • A real app built from made-up options, provideFirebaseApp(() => initializeApp({ apiKey: 'fake-api-key', projectId: 'demo-project' })). Creating services and references needs no network.
    • A small fake object provided for Auth through TestBed, for code that reads the signed-in state.
    • A service of your own around the AngularFire calls, replaced through TestBed providers.
    • The wrapper that @kekel87 posted above. It still works and the guide includes it as a plain object. Thank you for sharing it.

    The guide has the full examples. 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.

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: legacy (v7-v16)

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions