【问题标题】:Jest testing the same promise response in different types each time每次都在不同类型中测试相同的 Promise 响应
【发布时间】:2022-02-10 11:35:45
【问题描述】:

我如何正确地测试可以是不同类型的相同承诺响应?

例如,当从 Cloud Firestore 数据库中获取项目集合时,响应位于数组类型中,但有一种方法可以与响应在对象类型中进行交互。代码如下:

// ...
await Database.collection('products').select('published').get().then(snapshot => {
  snapshot.forEach(doc => { // <---- HERE snapshot IS AN ARRAY
    if (doc.data().published) {
      // ...
    }
  })
  console.log(snapshot.docs.length) // <---- HERE snapshot IS AN OBJECT. HOW ?
  console.log(snapshot.size) // IT'S THE SAME AS snapshot.docs.length
})
// ...

我试图通过在每次调用后返回一个不同的值来测试它,但它现在可以工作了,我收到一个错误: TypeError: Cannot read property 'length' of undefined:

jest.doMock('@google-cloud/firestore', () => class {
  constructor () {
    this.collection = jest.fn().mockImplementation((collectionName) => {
      if (collectionName === 'products') {
        return {
          select: () => ({
            get: jest.fn().mockResolvedValueOnce([ // <--- AN ARRAY
              {
                data: () => {
                  return {
                    published: false
                  }
                }
              }
            ]).mockResolvedValueOnce({ // <---- AN OBJECT
              docs: () => []
            })
          }),
          limit: () => ({ get: async () => ({ empty: true }) })
        }
      }
    })
  }
})

这里有什么问题?在所有这些嘲笑中我应该使用什么?:

  • mockImplementation
  • mockImplementationOnce
  • mockResolvedValue
  • mockResolvedValueOnce
  • mockReturnValue
  • mockReturnValueOnce

【问题讨论】:

    标签: node.js unit-testing google-cloud-firestore jestjs


    【解决方案1】:

    您不需要使用类来创建测试替身。它使事情变得复杂。只需使用 JS 普通对象数据类型 - Array 来制作 firestore 的查询快照。使用Object制作查询文档快照。

    您可以在 JS 数组中添加自定义属性,例如 snapshot.docssnapshot.size

    例如

    index.ts:

    import { Firestore } from '@google-cloud/firestore';
    
    export async function main() {
      const Database = new Firestore();
      await Database.collection('products')
        .select('published')
        .get()
        .then((snapshot) => {
          snapshot.forEach((doc) => {
            if (doc.data().published) {
              console.log(doc.data());
            }
          });
          console.log(snapshot.docs.length);
          console.log(snapshot.size);
        });
    }
    

    index.test.ts:

    describe('71058297', () => {
      beforeEach(() => {
        jest.resetModules();
      });
      test('should pass', async () => {
        const mDocument1 = { data: jest.fn(() => ({ published: true, name: 'steam deck' })) };
        const mQuerySnapshot: any = [mDocument1];
        mQuerySnapshot.docs = mQuerySnapshot;
        mQuerySnapshot.size = mQuerySnapshot.docs.length;
        const mFirestore = {
          collection: jest.fn().mockReturnThis(),
          select: jest.fn().mockReturnThis(),
          get: jest.fn().mockResolvedValueOnce(mQuerySnapshot),
        };
        const MockFireStore = jest.fn(() => mFirestore);
        jest.doMock('@google-cloud/firestore', () => ({ Firestore: MockFireStore }));
        const { main } = require('./');
        await main();
        expect(MockFireStore).toBeCalledTimes(1);
        expect(mFirestore.collection).toBeCalledWith('products');
        expect(mFirestore.select).toBeCalledWith('published');
        expect(mFirestore.get).toBeCalledTimes(1);
        expect(mDocument1.data).toBeCalledTimes(2);
      });
    });
    

    测试结果:

     PASS  stackoverflow/71058297/index.test.ts
      71058297
        ✓ should pass (24 ms)
    
      console.log
        { published: true, name: 'steam deck' }
    
          at stackoverflow/71058297/index.ts:33:17
              at Array.forEach (<anonymous>)
    
      console.log
        1
    
          at stackoverflow/71058297/index.ts:36:13
    
      console.log
        1
    
          at stackoverflow/71058297/index.ts:37:13
    
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        1.595 s, estimated 2 s
    

    【讨论】:

    • 非常感谢!如果有任何特殊的网站可以让我了解更多关于玩笑的信息,请分享链接。我从未见过您使用的这种方法,如此简单,无需复杂的模拟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2020-01-27
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多