'Class method is not mocking in Jest
I can't find a proper way to mock that line, I keep receiving following error messages:
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() })
Here is my test case:
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()
})
What should I use, to mock the results of that snapshot ? I tried this also but with no success:
.mockImplementationOnce({ docs: [] })
.mockResolvedValueOnce({ docs: [] })
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
