You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[docs] Mocking of angularfire methods with angularfire 7 during tests #3050
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).
Reacted by Thomas Mclennan, Michael Parry, Thiago Leite, Guillaume Fong-Ponne, Masayuki Sugahara, Jake Smith, Jesper Bager Rasmussen, Hassan Badat and Kayla Elortondo
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.
Activity
google-oss-bot commentedon Nov 8, 2021
This issue does not seem to follow the issue template. Make sure you provide all the required information.
kekel87 commentedon Jul 26, 2022
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:
AuthServiceto test :Units tests :
The logic remains the same for all functions exposed directly from version 7 of angular/fire.
docaohuynh commentedon Feb 6, 2023
Saved my day literally
Kwabena-Agyeman commentedon Aug 4, 2023
Dont know how you came up with this, but excellent job!!!
New to angular and this helped me out A LOT
docs: add Karma and Jasmine to the testing guide
armando-navarro commentedon Dec 1, 2026
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
spyOnon a function exported from@angular/fire/authstill fails, with<spyOn> : authState is not declared writable or has no setter. It fails the same way onfirebase/authitself, so my read is that it comes from how module exports are compiled and not from AngularFire.Either AngularFireModule has not been provided in your AppModule, is no longer in AngularFire.What works under Jasmine
provideFirebaseApp(() => initializeApp({ apiKey: 'fake-api-key', projectId: 'demo-project' })). Creating services and references needs no network.AuththroughTestBed, for code that reads the signed-in state.TestBedproviders.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.