【问题标题】:Class method is not mocking in Jest类方法在 Jest 中不模拟
【发布时间】: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


    【解决方案1】:
    
    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()
      })
    

    使用71115363 作为此答案的参考并进行一些修改。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2016-11-07
      • 2023-02-15
      • 1970-01-01
      • 2020-03-31
      • 2020-06-21
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      相关资源
      最近更新 更多