相关疑难解决方法(0)

如何用 Jest 模拟 Axios?

client/index.js我有一个使用 axios 发出请求的函数

import axios from "axios";

const createRequest = async (url, method) => {
    const response = await axios({
        url: url,
        method: method
    });
    return response;
};

export default { createRequest };
Run Code Online (Sandbox Code Playgroud)

我想使用 测试这个函数jest,所以我创建了client/index.test.js

import { jest } from "@jest/globals";
import axios from "axios";
    
import client from "./";

jest.doMock('axios', () => jest.fn(() => Promise.resolve()));

describe("Client", () => {

    it("should call axios and return a response", async () => {
        const response = await client.createRequest('http://localhost/', 'GET'); …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing node.js jestjs axios

13
推荐指数
2
解决办法
4万
查看次数

标签 统计

axios ×1

javascript ×1

jestjs ×1

node.js ×1

unit-testing ×1