How to mock Firestore in v9 #3095
Replies: 6 comments 11 replies
-
I am having the same issue I am trying to use https://github.com/ike18t/ng-mocks I look at the examples which @jamesdaniels gave, but it doesn't seem helpful: |
Beta Was this translation helpful? Give feedback.
-
But the advantage to Instead of using Besides that, I think it would be valuable for you to move all of your firebase logic to another service and out of the component layer.. that way you will simplify your component tests and just return "a fake observable" instead of dealing with the hard mocking of firebase. Same goes for the activatedRoue mocking - Whenever the tests are hard to write - it's often a signal that you might want to re-think your design decision Again, I don't know the entire context of your code, but these are just a few thoughts that might help |
Beta Was this translation helpful? Give feedback.
-
Hi, I tried so hard to mock v9 functions such as Spying the service or component under test with Finally, I decided to write full integration tests. This worked for me: Serviceimport { Injectable } from '@angular/core';
import { doc, docData, Firestore } from '@angular/fire/firestore';
import { traceUntilFirst } from '@angular/fire/performance';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private firestore: Firestore) { }
get(path: string, ...pathSegments: string[]): Observable<any> {
const ref = doc(this.firestore, path, ...pathSegments);
return docData(ref).pipe(traceUntilFirst('firestore'));
}
} Testsimport { TestBed } from '@angular/core/testing';
import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
import { environment } from 'src/environments/environment';
import { DataService } from './data.service';
describe('DataService', () => {
let service: DataService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
provideFirebaseApp(() => initializeApp(environment.firebase)),
provideFirestore(() => getFirestore())
]
});
service = TestBed.inject(DataService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should return the doc if path is valid', (done: DoneFn) => {
service
.get('items/1')
.subscribe((data: unknown) => {
expect(data).toBeDefined();
done();
});
});
it('should be undefined if path does not exist', (done: DoneFn) => {
service
.get('items/999999')
.subscribe((data: unknown) => {
expect(data).toBeUndefined();
done();
});
});
}); Output$npm test
> [email protected] test
> ng test --code-coverage --no-watch --browsers=FirefoxHeadless
✔ Browser application bundle generation complete.
01 05 2022 00:06:42.233:INFO [karma-server]: Karma v6.3.19 server started at http://localhost:9876/
01 05 2022 00:06:42.236:INFO [launcher]: Launching browsers FirefoxHeadless with concurrency unlimited
01 05 2022 00:06:42.243:INFO [launcher]: Starting browser FirefoxHeadless
01 05 2022 00:06:58.734:INFO [Firefox 91.0 (Linux x86_64)]: Connected on socket 56KwfxRN9Uw_24wzAAAB with id 11166302
Firefox 91.0 (Linux x86_64): Executed 4 of 4 SUCCESS (1.781 secs / 1.7 secs)
TOTAL: 4 SUCCESS
=============================== Coverage summary ===============================
Statements : 100% ( 8/8 )
Branches : 100% ( 0/0 )
Functions : 100% ( 2/2 )
Lines : 100% ( 6/6 )
================================================================================ I hope it helps. |
Beta Was this translation helpful? Give feedback.
-
Any update on this one? |
Beta Was this translation helpful? Give feedback.
-
Any progress here? |
Beta Was this translation helpful? Give feedback.
-
Exactly what I’m doing. But how do I test the service??
…On Thu, Jul 4, 2024 at 1:58 PM Mark Goho ***@***.***> wrote:
don't inject Firestore directly into your component, use a service, then
mock the service in the test
—
Reply to this email directly, view it on GitHub
<#3095 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAYKG34MGSPY7VHW3LXX74LZKWZPHAVCNFSM6AAAAABKMDKYROVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TSNRSGMZDS>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hello,
I am migrating to Angular 12, Angular Fire 7 and Firebase 9, but I am failing to mock firestore.
I've migrated all my code to the tree shakeable format: ie:
But now, my tests are failing w/ the message:
Error: AngularFireModule has not been provided at getScheduler
Here is an example:
Firestore snipet:
So what is the correct way to mock firestore in v9?
Beta Was this translation helpful? Give feedback.
All reactions