Javascript 来自节点模块的JEST模拟
我已经尝试了一段时间,但我找不到解决方案 我尝试了很多解决方案(mock文件夹、mockimplementation、mock等等),但我总是遇到同样的错误client。mockimplementation不是一个函数Javascript 来自节点模块的JEST模拟,javascript,node.js,mocking,jestjs,Javascript,Node.js,Mocking,Jestjs,我已经尝试了一段时间,但我找不到解决方案 我尝试了很多解决方案(mock文件夹、mockimplementation、mock等等),但我总是遇到同样的错误client。mockimplementation不是一个函数 //restclient.js module.exports=(cfg)=>新客户端(配置); //api.js const client=require('restclient'); module.exports.doRequest=()=>{ const request=c
//restclient.js
module.exports=(cfg)=>新客户端(配置);
//api.js
const client=require('restclient');
module.exports.doRequest=()=>{
const request=client();
const config={};
get('/path/to/request',config)
.then(result=>console.log(result))
}
//api.tests.js
const client=require('restclient');
常量api=要求('./api');
jest.mock('restclient',()=>()=>({
get:jest.fn(),
}));
描述('测试API',()=>{
test('testthen',async()=>{
试一试{
restclient.mockImplementation(()=>()=>({
get:(url,config)=>“嗨!我被嘲笑了”,
}));
const result=await api.doRequest();
console.log('result',result);
}捕获(e){
console.log('eee',e);
}
});
});
我找不到解决方案,我想我无法模拟
const request=restclient()
部分,但我不知道为什么 您没有模拟构造函数
这模拟了restclient
jest.mock('restclient', ()=> jest.fn().mockImplementation(() => {
/** here you can create and return a mock of request **/
}));
您没有模拟构造函数 这模拟了
restclient
jest.mock('restclient', ()=> jest.fn().mockImplementation(() => {
/** here you can create and return a mock of request **/
}));