Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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
Node.js 如何从Jest测试页面点击axios请求_Node.js_Reactjs_Jestjs - Fatal编程技术网

Node.js 如何从Jest测试页面点击axios请求

Node.js 如何从Jest测试页面点击axios请求,node.js,reactjs,jestjs,Node.js,Reactjs,Jestjs,我正在尝试从我的code.test.js文件中点击axios请求: import axios from 'axios' import sinon from 'sinon'; describe('get-data', () => { let data = {start_date:"2017-06-30",end_date:"2017-07-07",graph:"all"} let sandbox; let server; beforeEach(() => { sandbo

我正在尝试从我的code.test.js文件中点击axios请求:

import axios from 'axios'
import sinon from 'sinon';

describe('get-data', () => {

let data = {start_date:"2017-06-30",end_date:"2017-07-07",graph:"all"}

let sandbox;
let server;
beforeEach(() => {
    sandbox = sinon.sandbox.create();
    server = sandbox.useFakeServer();
});
afterEach(() => {
    server.restore();
    sandbox.restore();
});

it('should display a blankslate', (done) => {
    axios.get('/api/get/data?data='+JSON.stringify(data))
        .then((response.data) => {
        console.log(response)
            /*expect($('#users').innerHTML)
            .to.equal('The list is empty.')*/ })
        .then(done, done);
    setTimeout(() => server.respond([200,
        { 'Content-Type': 'application/json' },
    '[]']), 0);
});
})
但我得到的console.log(response.data)是未定义的


有谁能告诉我如何在这里获取数据作为响应吗?

从技术上讲,在测试中,您不应该实际执行请求,而应该模拟请求并测试副作用

但是,如果我没记错的话,默认情况下,
jest
会模仿一切,除非告诉我不要太多。在
package.json
中添加以下部分和内容:


{
“笑话”:{
“unmockedModulePathPatterns”:[
“axios”,
]
}
}

这将允许您在测试中根据需要执行实际的网络请求。如果要禁用自动模拟,还可以将
“automock”:true、
属性传递到
jest
部分

文档:


<>代码>响应。数据< /代码>未定义,虽然<>代码>响应<代码>中的其他字段正在正确地出现,但默认情况下启用了自动锁存,但现在从V15开始禁用。