【发布时间】:2022-02-22 06:40:34
【问题描述】:
我找不到模拟该行的正确方法,我不断收到以下错误消息:
console.log
models // <---- THIS IS A LINE 173
TypeError: Database.collection(...).onSnapshot is not a function
172 | collections.forEach(collection => {
173 | console.log(collection)
> 174 | Database.collection(collection).onSnapshot(snapshot => {
| ^
175 | console.log('????', snapshot) // <---- I WANT TO MOCK THIS snapshot
176 | snapshot.docChanges().forEach(change => {
177 | const item = Object.freeze({ id: change.doc.id, ...change.doc.data() })
这是我的测试用例:
test.only('should react when a new record is created', async () => {
jest.doMock('@google-cloud/firestore', () => jest.fn(() => ({
collection: jest.fn()
.mockReturnThis(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockResolvedValueOnce(() => { // <---- AT THIS POINT, LINE 174 SHOULD BE MOCKED
return {
onSnapshot: jest.fn().mockReturnValue({
snapshot: []
})
}
}),
get: jest.fn().mockResolvedValue({ docs: [] }),
doc: jest.fn().mockReturnThis(),
set: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
onSnapshot: jest.fn().mockReturnThis().mockImplementationOnce(function () { return this }),
select: jest.fn().mockReturnThis()
})))
const StatisticsService = require('./index')
await StatisticsService()
})
我应该使用什么来模拟该快照的结果?我也试过了,但没有成功:
.mockImplementationOnce({ docs: [] })
.mockResolvedValueOnce({ docs: [] })
【问题讨论】:
标签: node.js unit-testing google-cloud-firestore jestjs