Jest axios 模拟在预期响应中返回 undefined

Jest axios mock returning undefined in expected response

似乎通过 axios 模拟对我不起作用,因为它似乎实现了模拟功能。我不确定为什么我不能嘲笑 axios.Can 有人指出我犯的错误吗?过去 3 天我一直在尝试这个。

index.test.ts

从“axios”导入 axios; // 从节点模块导入 jest.mock("axios");

describe("compositeScore()", () => {

    it("Mock Fetch API for Composite Score Response", async () => {
        
        axios.post = jest.fn().mockResolvedValue(mockResponse);
        const response = await dateFilter(platform);
        expect(axios.post).toHaveBeenCalledTimes(1);
        expect(response).toEqual(mockFetchCompositeScoreResponse);
    });
});

index.ts

export const dateFilters = async (platform) => {
    const dates = await fetchWrapper(
        platform.toLowerCase().concat("DateFilters"),
        platform,
        {}
    );
    return dates;
};


export async function fetchWrapper(
    queryName: string,
    platform: string,
    queryParams?: {}
) {
   
    const headers = {
        Accept: "application/json",
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json",
     
    };
    const config: AxiosRequestConfig = {
        method: "post",
        url,
        headers,
        data: {
            db: dbName,
            csl: queryParams
                ? substituteQueryParameters(queries[queryName], queryParams)
                : queries[queryName],
        },
    };

    return axios(config);
}

我收到错误信息 预期调用次数:1 接听电话数:0

并且还 Received : undefined 和 expected 存储 mockResponse。

axios 函数发出的 HTTP 请求,而非 axios.post 方法。

例如

index.ts:

import axios, { AxiosRequestConfig } from 'axios';

const token = '123';
const url = 'http://localhost:8080/api';
const dbName = 'arena';

export const dateFilters = async (platform) => {
  return fetchWrapper(platform.toLowerCase().concat('DateFilters'), platform, {});
};

export async function fetchWrapper(queryName: string, platform: string, queryParams?: {}) {
  const headers = {
    Accept: 'application/json',
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/json',
  };
  const config: AxiosRequestConfig = { method: 'post', url, headers, data: { db: dbName, csl: queryParams } };
  return axios(config);
}

index.test.ts:

import { dateFilters } from './';
import axios from 'axios';

jest.mock('axios');
const mAxios = axios as jest.MockedFunction<typeof axios>;

describe('71387289', () => {
  test('should pass', async () => {
    const mockResponse = { data: 'fake data', status: 200, statusText: 'ok', headers: {}, config: {} };
    mAxios.mockResolvedValueOnce(mockResponse);
    const platform = 'awesome';
    const actual = await dateFilters(platform);
    expect(axios).toHaveBeenCalledTimes(1);
    expect(actual).toEqual(mockResponse);
  });
});

测试结果:

 PASS  Whosebug/71387289/index.test.ts (7.78 s)
  71387289
    ✓ should pass (4 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.ts |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        7.906 s, estimated 10 s