[英]How to mock Firestore v9 getDocs() with Jest
We recently upgraded our webapp from Firebase v8 to v9, and got all kind of problems from the new syntax.我们最近将我们的 webapp 从 Firebase v8 升级到 v9,并从新语法中遇到了各种问题。 I am still quite new to Jest and Firebase/Firestore, so not everything is crystal clear to me yet...我对 Jest 和 Firebase/Firestore 还是很陌生,所以我还不是很清楚所有的事情......
I am trying to mock getDocs
from firebase/firestore
because I get this error message:我正在尝试从 firebase firebase/firestore
模拟getDocs
,因为我收到此错误消息:
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 happens when testing gets into the following code:当测试进入以下代码时发生错误:
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
is someting like: getDataCollection
有点像:
export const getDataCollection = (): CollectionReference =>
collection(
firestoreInstance,
`path-to-collection`
)
Because getDocs is not mocked.因为 getDocs 没有被嘲笑。 If I mock getDocs
like this:如果我这样模拟getDocs
:
;(getDocs as any).mockImplementation(() => Promise.resolve({
docs: {
data: () => ({
name: 'name-for-this-data-item',
}),
},
}))
I get the following error in the Jest output for this test:对于此测试,我在 Jest output 中收到以下错误:
TypeError: _firestore.getDocs.mockImplementation is not a function
Mocking Firebase v9 seems to be hard from all the unanswered questions I have seen on the inte.net and I did not find any direct answer to my problem. Mocking Firebase v9 似乎很难从我在 inte.net 上看到的所有未回答的问题中找到,我没有找到我的问题的任何直接答案。
Any idea about what I am doing wrong?知道我做错了什么吗? And any pointer to how to solve my problem?以及如何解决我的问题的任何指示?
As often, I hesitate to ask questions here, and when I do, I often end up finding an answer myself soon after...像往常一样,我在这里犹豫要不要提问,而当我这样做时,我通常很快就会自己找到答案......
So to put it simply: no need to mock getDocs
!所以简单地说:不需要模拟getDocs
!
Instead, the mock for the collection get function had to be modified from:相反,集合 get function 的模拟必须修改为:
;(getDataCollection as any).mockImplementation(() => ({
docs: {
data: () => ({
name: 'name-for-this-data-item',
}),
},
}))
To:到:
;(getDataCollection as any).mockImplementation(() => ({
query: {
docs:[{
data: () => ({
id: 'id-for-this-data-item',
name: 'name-for-this-data-item',
}),
}],
},
}))
})
To add a level for the query
function.为query
function 添加级别。
I also added an id
field in the data to correct a second UNRELATED error in my algorithm.我还在数据中添加了一个id
字段,以更正我算法中的第二个无关错误。
I hope that helps other Jest/Firebase/Firestore newbies like me.我希望这能帮助像我这样的其他 Jest/Firebase/Firestore 新手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.