【发布时间】:2019-03-22 09:34:52
【问题描述】:
我在尝试使用构造函数模拟模块时遇到问题
// code.js
const ServiceClass = require('service-library');
const serviceInstance = new ServiceClass('some param');
exports.myFunction = () => {
serviceInstance.doSomething();
};
以及测试代码:
// code.test.js
const ServiceClass = require('service-library');
jest.mock('service-library');
const {myFunction} = require('../path/to/my/code');
test('check that the service does something', () => {
// ????
});
这与文档示例Mocking Modules 不同,因为您需要在导入模块后对其进行实例化。而且也不像模拟函数。
我如何在测试时模拟这个 doSomething() 函数?
作为参考,我在这里尝试模拟 @google-cloud/* 包。我有几个项目可以利用这一点。
【问题讨论】:
标签: node.js unit-testing google-cloud-platform mocking jestjs