【发布时间】: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