Class 方法在 Jest 中不是模拟
Class method is not mocking in Jest
我找不到模拟该行的正确方法,我不断收到以下错误消息:
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: [] })
test('should react when a new record is created', async () => {
jest.doMock('@google-cloud/firestore', () => {
const mChanges = [
{
type: 'added',
doc: {
id: 'MOD_1234',
data: () => ({
sku: 'SKU_1234',
model: 'MOD_1234',
updatedAt: {
_seconds: Math.round(Date.now() / 1000) + 1000
}
})
}
}
]
const mSnapshot = {
docChanges: jest.fn().mockReturnValue(mChanges)
}
return jest.fn(() => ({
collection: jest.fn().mockReturnThis(),
get: jest.fn().mockResolvedValue({ docs: [] }),
doc: jest.fn().mockReturnThis(),
set: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
onSnapshot: jest.fn().mockImplementation(callback => {
callback(mSnapshot)
})
}))
})
const Service = require('./index')
const Module = new Service()
jest.spyOn(Module, 'onDatabaseRecordCreated')
const consoleLog = jest.spyOn(console, 'log').mockImplementation()
await Module.init()
consoleLog.mockRestore()
expect(Module.onDatabaseRecordCreated).toHaveBeenCalled()
})
使用 作为此答案的参考并进行了一些修改。
我找不到模拟该行的正确方法,我不断收到以下错误消息:
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: [] })
test('should react when a new record is created', async () => {
jest.doMock('@google-cloud/firestore', () => {
const mChanges = [
{
type: 'added',
doc: {
id: 'MOD_1234',
data: () => ({
sku: 'SKU_1234',
model: 'MOD_1234',
updatedAt: {
_seconds: Math.round(Date.now() / 1000) + 1000
}
})
}
}
]
const mSnapshot = {
docChanges: jest.fn().mockReturnValue(mChanges)
}
return jest.fn(() => ({
collection: jest.fn().mockReturnThis(),
get: jest.fn().mockResolvedValue({ docs: [] }),
doc: jest.fn().mockReturnThis(),
set: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
onSnapshot: jest.fn().mockImplementation(callback => {
callback(mSnapshot)
})
}))
})
const Service = require('./index')
const Module = new Service()
jest.spyOn(Module, 'onDatabaseRecordCreated')
const consoleLog = jest.spyOn(console, 'log').mockImplementation()
await Module.init()
consoleLog.mockRestore()
expect(Module.onDatabaseRecordCreated).toHaveBeenCalled()
})
使用