After upgrading our webapp from Firebase v8 to v9, we encountered various issues due to the new syntax. As I am still relatively new to Jest and Firebase/Firestore, not everything is completely clear to me yet ...
I am attempting to mock getDocs
from firebase/firestore
because of the error message I am receiving:
TypeError: Cannot read properties of undefined (reading 'docs')
at /.../src/.../operations.ts:123:45
at processTicksAndRejections (node:internal/process/task_queues:34:5)
at Object.<anonymous> (/.../src/.../index.test.ts:1234:6)
The error occurs during testing when the code reaches the following section:
const firestoreInstance = getFirestore(firebaseApp)
...
const dataList = query(
getDataCollection(),
where('id', 'in', idList)
)
const dataDict: { [key: string]: {id: string, name: string} } = {}
;(await getDocs(dataList)).docs.forEach(
(dataItem) => {
const data = dataItem.data()
dataDict[data['id']].name = data.name
}
)
Where getDataCollection
resembles something like:
export const getDataCollection = (): CollectionReference =>
collection(
firestoreInstance,
`path-to-collection`
)
Due to getDocs not being mocked, when I attempt to mock it in this manner:
;(getDocs as any).mockImplementation(() => Promise.resolve({
docs: {
data: () => ({
name: 'name-for-this-data-item',
}),
},
}))
I encounter the following error in the Jest output for this particular test:
TypeError: _firestore.getDocs.mockImplementation is not a function
Mocking Firebase v9 appears challenging based on the unanswered queries I have come across online, and I have yet to find a direct solution to my issue.
Any insights into what I may be doing incorrectly? And any guidance on how to resolve this problem?