-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfirebaseMocks.ts
40 lines (37 loc) · 1.31 KB
/
firebaseMocks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as app from 'firebase-admin/app';
import * as firestore from 'firebase-admin/firestore';
import * as auth from 'firebase-admin/auth';
// Test config (fill out with your test project values)
const config = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: "",
};
export const mockFirebase = () => {
jest.doMock('firebase-admin/firestore', () => {
const apps = app.getApps();
const currentApp = apps.length === 0 ? app.initializeApp(config) : apps[0] ?? undefined;
return {
getFirestore: jest.fn(() => firestore.getFirestore(currentApp)),
FieldValue: firestore.FieldValue,
Timestamp: firestore.Timestamp,
Transaction: firestore.Transaction,
DocumentReference: firestore.DocumentReference,
}
});
jest.doMock('firebase-admin/auth', () => {
const apps = app.getApps();
const currentApp = apps.length === 0 ? app.initializeApp(config) : apps[0] ?? undefined;
return {
getAuth: jest.fn(() => auth.getAuth(currentApp)),
}
});
jest.doMock('firebase-admin/app', () => ({
initializeApp: jest.fn(() => app.initializeApp(config)),
getApps: jest.fn(() => app.getApps()),
}));
};