gpt4 book ai didi

reactjs - 为什么我得到 : TypeError: _axios. default.get.mockResolvedValue is not a function

转载 作者:行者123 更新时间:2023-12-04 13:14:12 27 4
gpt4 key购买 nike

这是错误:

    TypeError: _axios.default.get.mockResolvedValue is not a function

20 | ]
21 | }
> 22 | Axios.get.mockResolvedValue(response);
| ^
23 |
24 | const component = renderer.create(
25 | <BookList />

这是我的代码:

import React from "react";
import Axios from "axios";
import {shallow, mount, render} from "enzyme";

import BookList from "../BookList";

import renderer from "react-test-renderer";

jest.mock('Axios');

test("<BookList /> snapshot test", () => {
const response = {
data: [
{
title: "foo"
},
{
title: "bar"
}
]
}
Axios.get.mockResolvedValue(response);

const component = renderer.create(
<BookList />
)
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
})

最佳答案

您收到此错误是因为 Axios.post 中不存在 mockResolvedValue。您必须根据需要输入大小写

(Axios.get as jest.Mock<{}>).mockResolvedValue(response);

此外,请确保根据需要初始化 Axios.get 的值。喜欢

Axios.get = jest.fn()

还有

jest.mock('Axios');

必须是

jest.mock('axios');

关于reactjs - 为什么我得到 : TypeError: _axios. default.get.mockResolvedValue is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62026593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com