【问题标题】:How to mock Google Cloud Firestore Fieldvalue?如何模拟 Google Cloud Firestore 字段值?
【发布时间】:2021-07-19 23:06:57
【问题描述】:

我在我的 NodeJS 项目中使用@google-cloud/firestore 包,我正在尝试使用 Jest 设置一些单元测试。我目前正在像这样模拟包(使用 __mocks__ 文件夹中的手动模拟):

const mockFirestore = {
  collection: jest.fn(() => mockCollection),
};

module.exports = {
  Firestore: jest.fn(() => mockFirestore),
};

这非常适合模拟查询,但是当我尝试调用一个使用 Firestore FieldValue 的函数时遇到了问题:

import {Firestore} from '@google-cloud/firestore';
const firestore = new Firestore();

const main = async () => {
  await firestore
    .collection('foo')
    .doc('bar')
    .set({
      foo: 'baz', 
      updated: Firestore.FieldValue.serverTimestamp()
    }, {merge: true});
}

main();

关于如何模拟这个的任何想法或最佳实践? 谢谢!

【问题讨论】:

  • 你是如何使用@google-cloud/firestore 包的?显示被测代码
  • 添加了更多代码@slideshowp2
  • 您是否在模块范围内使用await 语法?
  • 在 sn-p 中是的,在我的代码中没有。我现在更新了 sn-p,但我认为特定方面与我关于如何模拟 FieldValue 的问题并不真正相关。当 Firestore 已经被模拟为一个函数时,我正在专门寻找一种模拟 Firestore.FieldValue 的方法。 @slideshowp2

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


【解决方案1】:

只需将模拟的静态属性FieldValue 和方法分配给Firestore 类。使用mock.mockReturnValueOnce()方法模拟.serverTimestamp()方法的返回值,用于不同的测试用例。

__mocks__/@google-cloud/firestore/index.js:

const mockFirestore = {
  collection: jest.fn().mockReturnThis(),
  doc: jest.fn().mockReturnThis(),
  set: jest.fn(),
};

const MockFirestore = jest.fn(() => mockFirestore);
MockFirestore.FieldValue = {
  serverTimestamp: jest.fn(),
};

module.exports = {
  Firestore: MockFirestore,
};

main.js:

import { Firestore } from '@google-cloud/firestore';
const firestore = new Firestore();

const main = async () => {
  await firestore.collection('foo').doc('bar').set(
    {
      foo: 'baz',
      updated: Firestore.FieldValue.serverTimestamp(),
    },
    { merge: true }
  );
};

export { main };

main.test.js:

import { main } from './main';
import { Firestore } from '@google-cloud/firestore';

jest.mock('@google-cloud/firestore');

describe('67268943', () => {
  it('should pass', async () => {
    Firestore.FieldValue.serverTimestamp.mockReturnValueOnce(2021);
    expect(Firestore).toBeCalledTimes(1);
    await main();
    const firestore = new Firestore();
    expect(firestore.collection).toBeCalledWith('foo');
    expect(firestore.doc).toBeCalledWith('bar');
    expect(firestore.set).toBeCalledWith(
      {
        foo: 'baz',
        updated: 2021,
      },
      { merge: true }
    );
  });
});

单元测试结果:

 PASS  examples/67268943/main.test.js (10.868 s)
  67268943
    ✓ should pass (4 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 main.js  |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        12.589 s

【讨论】:

  • 这个答案对你有帮助吗?
猜你喜欢
  • 2020-06-18
  • 1970-01-01
  • 2019-07-18
  • 2018-03-27
  • 1970-01-01
  • 2019-07-18
  • 2020-04-25
  • 1970-01-01
  • 2019-08-23
相关资源
最近更新 更多