Javascript JEST TypeError:无法读取属性';json';未定义的

Javascript JEST TypeError:无法读取属性';json';未定义的,javascript,node.js,jestjs,Javascript,Node.js,Jestjs,我在这里非常需要帮助。我试着开玩笑地测试一个函数,但有一件事我被卡住了。当我尝试模拟提取请求时,会出现以下错误: TypeError: Cannot read property 'json' of undefined 我要测试的功能是: const updateUI = async () =>{ const res = await fetch('/sentiment'); console.log(res); try { console.log(re

我在这里非常需要帮助。我试着开玩笑地测试一个函数,但有一件事我被卡住了。当我尝试模拟提取请求时,会出现以下错误:

TypeError: Cannot read property 'json' of undefined
我要测试的功能是:

const updateUI = async () =>{
    const res = await fetch('/sentiment');
    console.log(res);
    try {
        console.log(res.data)
        const allData = await res.json();
        console.log(allData)
        document.getElementById("polarity").innerHTML = allData.polarity;
        document.getElementById("polarityConfidence").innerHTML = allData.polarity_confidence;
        document.getElementById("subjectivity").innerHTML = allData.polarity;
        document.getElementById("subjectivityConfidence").innerHTML = allData.polarity_confidence;
        return allData;
    }catch(error){
        console.log('error')
    }
};

export { updateUI }
我尝试运行的测试是:

import "regenerator-runtime/runtime";
import "core-js/stable";
import "fetch-mock"

const fetchMock = require('fetch-mock');
fetchMock.config.sendAsJson = true;  \\I've tried with and without this part and I get the same error

import updateUI from './updateUI';
import { isIterable } from "core-js";

describe('updateUI', () => {
    it('can fetch', async () => {
        fetchMock.get('/sentiment', {polarity: 'polarity', polarity_confidence: 'polarity confidence', subjectivity: 'subjectivity', subjectivity_confidence: 'subjectivity confidence'});
        const res = await updateUI('/sentiment');
        const allData = await res.json();
        expect(allData.polarity).toEqual('polarity');
        expect(allData.polarity_confidence).toEqual('polarity confidence');
        expect(allData.subjectivity).toEqual('subjectivity');
        expect(allData.subjectivity_confidence).toEqual('subjectivity confidence');

    })
})

我真的不知道从这里到哪里去。为什么它不能得到json对象?是因为updateUI函数调用函数try{}部分的json对象吗?如果是这种情况,我该如何测试它?

我在这里看到两个问题

  • 在测试中,您传递的字符串如下所示:
    const res=await updateUI('/touction')这无关紧要,因为updateUI不接受任何参数

  • 在下一行中,您正在执行
    res.json()
    ,这与实际方法不同,您只返回响应。在测试中,您不需要执行
    .json()
    。这就是为什么没有定义json函数的原因


    • 我发现这里有两个问题

      • 在测试中,您传递的字符串如下所示:
        const res=await updateUI('/touction')这无关紧要,因为updateUI不接受任何参数

      • 在下一行中,您正在执行
        res.json()
        ,这与实际方法不同,您只返回响应。在测试中,您不需要执行
        .json()
        。这就是为什么没有定义json函数的原因