import { DocumentReference, DocumentData, Firestore, CollectionReference } from…
- User
import { DocumentReference, DocumentData, Firestore, CollectionReference } from '@google-cloud/firestore';
// Define a mocked transaction interface export interface MockedTransaction { set: jest.Mock; get: jest.Mock; }
// Define a mocked collection reference interface export interface MockedCollectionReference { doc: jest.Mock<jest.Mocked<DocumentReference>, [string]>; where: jest.Mock; get: jest.Mock; }
// Define a mocked Firestore interface export interface MockedFirestore { collection: jest.Mock<MockedCollectionReference, [string]>; runTransaction: jest.Mock< Promise, [ ( transaction: MockedTransaction ) => Promise ] >; FieldValue: { serverTimestamp: jest.Mock<Date, []>; }; doc: jest.Mock<jest.Mocked<DocumentReference>, [string]>; }
// Mock implementations for query methods const whereFunction = jest.fn().mockReturnThis(); const getFunction = jest.fn().mockResolvedValue({ empty: true });
// Define a mocked DocumentReference const docMock: jest.Mocked<DocumentReference> = { id: 'test-game-id', firestore: {} as jest.Mocked, parent: {} as jest.Mocked<CollectionReference>, path: 'games/test-game-id', collection: jest.fn(), set: jest.fn(), update: jest.fn(), delete: jest.fn(), get: jest.fn(), onSnapshot: jest.fn(), withConverter: jest.fn(), };
// Mock Firestore collection const collection = jest.fn<MockedCollectionReference, [string]>(() => ({ doc: jest.fn().mockReturnValue(docMock), where: whereFunction, get: getFunction, }));
// Mock runTransaction const runTransaction = jest.fn().mockImplementation(async ( updateFunction: (transaction: MockedTransaction) => Promise ) => { const transaction: MockedTransaction = { set: jest.fn(), get: jest.fn().mockResolvedValue({ data: () => ({}) }), }; return updateFunction(transaction); });
// Mock FieldValue.serverTimestamp const serverTimestamp = jest.fn(() => new Date());
// Mock Firestore instance const firestore: MockedFirestore = { collection, runTransaction, FieldValue: { serverTimestamp, }, doc: jest.fn().mockReturnValue(docMock), };
// Mock admin SDK const admin = { apps: [], initializeApp: jest.fn(), firestore: jest.fn(() => firestore), };
export default admin;
error TS2322: Type '{ id: string; firestore: jest.Mocked; parent: jest.Mocked<CollectionReference<DocumentData, DocumentData>>; ... 7 more ...; withConverter: jest.Mock<...>; }' is not assignable to type 'Mocked<DocumentReference<DocumentData, DocumentData>>'. Type '{ id: string; firestore: Mocked; parent: Mocked<CollectionReference<DocumentData, DocumentData>>; ... 7 more ...; withConverter: Mock<...>; }' is missing the following properties from type '{ readonly id: string; readonly firestore: Firestore; readonly parent: CollectionReference<DocumentData, DocumentData>; ... 10 more ...; withConverter: MockInstance<...>; }': listCollections, create, isEqual
38 const docMock: jest.Mocked<DocumentReference<DocumentData>> = { - Assistant
The error you're encountering is because the mock object
docMockis missing some methods that are required by theDocumentReferenceinterface, such aslistCollections,create, andisEqual. To fix the error, you need to include these missing methods in your mock object.Here's how you can modify your
docMockto include the missing methods:// Define a mocked DocumentReference const docMock: jest.Mocked<DocumentReference<DocumentData>> = { id: 'test-game-id', firestore: {} as jest.Mocked<Firestore>, parent: {} as jest.Mocked<CollectionReference<DocumentData>>, path: 'games/test-game-id', collection: jest.fn(), set: jest.fn(), update: jest.fn(), delete: jest.fn(), get: jest.fn(), onSnapshot: jest.fn(), withConverter: jest.fn(), // Add the missing methods listCollections: jest.fn(), create: jest.fn(), isEqual: jest.fn(), };