【发布时间】:2021-03-17 12:13:30
【问题描述】:
我正在尝试测试某些东西,但我无法找到解决方案
const Config = require('client-config')
const cfgM = Config.CFGBuilder.builder().build();
class ConfigService {
static get(key) {
return cfgM.getProperty(key);
}
}
module.exports = ConfigService;
这就是我想要测试的。我的 configManager 中的 get。
const Config = require('my-config');
const { expect } = chai;
const ConfigService = require('../ConfigService');
jest.mock('my-config', () => ({
CFGBuilder:
{
builder: () => ({
build: () => ({
getProperty: jest.fn(),
}),
}),
},
}));
describe('testing config service', () => {
describe('testing get', () => {
it('should return db.name', () => {
Config.CFGBuilder.builder().build().getProperty.mockImplementation(key => 'mykey');
expect(ConfigService.get('key')).to.be('mykey');
});
});
});
事情是我从ConfigServivce.get('key')得到未定义
免责声明:我没有编写客户端配置模块。这只是我必须使用的一个模块
【问题讨论】:
标签: node.js unit-testing jestjs mocking