簡體   English   中英

在這種情況下,開玩笑地測試axios功能的正確方法是什么?

[英]what is the Proper way of testing axios functions in jest in this case?

我已經寫了一個基本的測試用例來測試我的axios功能。 測試用例失敗。 我正在嘗試測試axios調用,該調用使我得到一個json。 我格式化該數據並解決Promise函數。 開玩笑地給出錯誤, TypeError: Cannot read property 'then' of undefined

`

TypeError:無法讀取未定義的屬性“ then”

  67 |                 'Authorization': `Bearer ${accessToken}`,
  68 |             },
> 69 |         }).then((response: any): any => {
     |           ^
  70 |             if (response.statusText === "OK") {
  71 |                 return Promise.resolve(response.data);
  72 |             }

`

嘲笑 /axios.ts

export default {
    get: jest.fn(() => Promise.resolve()),
    request: jest.fn(() => Promise.resolve())
};

// libHelper.tsx

export default class libHelper{
    constructor() {
        // parameters initialization
        }
    }

    public fetchMsGraph = (accessToken: string): Promise<GraphData> => {
        return axios.get(Constants.someURL, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${accessToken}`,
            },
        }).then((response: any): any => {
            if (response.statusText === "OK") {
                return Promise.resolve(response.data);
            }
            else {
                return Promise.reject(Error(`API Response not OK`));
            }
        }).catch((err: any): any => Promise.reject(Error(`Fetch API Error ${err}`)));

    }

// libHelper-tests.tsx

import libHelper from "../Scripts/libHelper";
import axios from "../__mocks__/axios";
import { UserDataGen1, Data } from "../Scripts/interfaces";

describe("", (): void => {

    test("basic test", () => {
        let msalobject: MsalHelper = new MsalHelper();
        expect(msalobject).toBeInstanceOf(MsalHelper);
    });

    test("not so basic", async () => {
        const resp: UserDataGen1 = {
            idp: "string",
            memberName: "string",
            lastName: "string",
            firstName: "string",
            cid: "string",
            authenticatedState: "string",
            picture: "string"
        }
        axios.get.mockImplementation(() => { Promise.resolve(resp); });

        let libObject: libHelper = new libHelper();
        const rand: Data = await libObject.fetchAPP("asd");

        expect(rand).toEqual(resp);
    });
});

您應該使用mockResolvedValue

       const resp: UserDataGen1 = {
            idp: "string",
            memberName: "string",
            lastName: "string",
            firstName: "string",
            cid: "string",
            authenticatedState: "string",
            picture: "string"
        }
        axios.get.mockResolvedValue(resp);

這就是它通過的原因

import mockAxios from "../__mocks__/axios";
const data: UserDataGen1 = {
            idp: "string",
            memberName: "string",
            lastName: "string",
            firstName: "string",
            cid: "string",
            authenticatedState: "string",
            picture: "string"
        }
    const resp: any = {
            data: data,
            status: 200,
            statusText: "OK",        
    }

    mockAxios.get.mockImplementation((url: string) =>{
                console.log(url);
                return Promise.resolve(resp);
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM