typescript - vitest mock tidak dapat memalsukan kaedah isAxiosError milik axios


Saya mempunyai sebuah fungsi yang dipanggil myfunction yang menggunakan axios untuk memanggil titik akhir API. Berikut adalah pelaksanaan saya dalam fail myfunction.ts:

export async function myfunction(request:any) {
    const url = 'https://sample.to';

    try {
            const response = await axios.post(url, request), {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
    });
    const { value } = response.data;
    return value
    }
    catch (error: unknown) {
        if (axios.isAxiosError(error)) {
            if (error.response?.status === 400) {
                throw new customError({
                    type: 'bad request',
                    status: 400
                });
            } else if (error.response?.status === 401) {
                throw new customError({
                    type: 'unauthorized',
                    status: 401
                });
            }
        }
        else{
            throw new customError({
                type: 'unknown error',
                status: 500
            });
        }
    }
}

Berikut ialah fail myfunction.spec.ts yang mengandungi kes ujian dan pelaksanaan tiruan:

import { beforeEach, describe, expect, it,vi } from 'vitest';
import { myfunction } from './myfunction.js';
import axios, { AxiosResponse } from 'axios'

vi.mock("axios", () => {
    return {
      default: {
        post: vi.fn(),
        isAxiosError:vi.fn().mockReturnValue(true),
      },
    };
  });

describe('myfunction test cases', () => {

    beforeEach(() => {
        vi.resetAllMocks();

    });

    it('should throw a customError when axios.post returns a 400 error', async () => {
        const mockResponse: AxiosResponse= {
            status:400,
            statusText:'Bad Request',
            headers:{},
            config:{},
            data:{customErrorData:{
                status: 400,
                type: 'bad request',
            },
            },
        }
        
        // Mock the axios.post method to throw an error
        const errorResponse = {
            response: mockResponse,
            isAxiosError: true,
        };
        const requestMock={id:'xyz',refer:'sample'}
        vi.spyOn(axios,'post').mockRejectedValueOnce(errorResponse);
        await expect(requestOboToken(requestMock)).rejects.toMatchObject(mockResponse.data);
        expect(axios.post).toHaveBeenCalledTimes(1);
    });
});

Ujian unit saya sentiasa mengembalikan ralat 500. Nampaknya ia tidak dapat melepasi kondisi if menggunakan isAxiosError untuk memeriksa ralat axios. Ada cadangan bagaimana untuk membetulkannya.