Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 来自节点模块的JEST模拟_Javascript_Node.js_Mocking_Jestjs - Fatal编程技术网

Javascript 来自节点模块的JEST模拟

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

我已经尝试了一段时间,但我找不到解决方案

我尝试了很多解决方案(mock文件夹、mockimplementation、mock等等),但我总是遇到同样的错误client。mockimplementation不是一个函数

//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 **/

}));