मज़ाक परीक्षण समूह के async के साथ कार्यों वादा करता हूँ । सभी

0

सवाल

मैं कोशिश कर रहा हूँ का परीक्षण करने के लिए इन कार्यों, वहाँ है, लेकिन एक ही लाइनों है कि मैं को कवर नहीं कर सकते,

userDetail.ts

export const getUser = async (email: string) => {
    const { data } = await axios.get(config.apiUrl + "/user");
    return data;
};

export const getOrganization = async (email: string) => {
    const { data } = await axios.get(config.apiUrl + "/organization");
    return data;
};

export const getKeys = async (org: string) => {
    const { data } = await axios.get(
        config.apiUrl + "/keys/" + org
    );

    return data;
};

// This is the part that can't get covered with the tests
const getUserDetail = async (email: string) => {
    const [user, org] = await Promise.all([getUser(email), getOrganization(email)]);
    const keys = await getKeys(org);
    return {
        user: user,
        roles: [user.role],
        orgs: (org.items || []).filter((orgItem: OrgItem) => {
            return orgItem.active;
        }),
        keys: keys.items,
    };
};

export default getUserDetail;

यह मेरे परीक्षण, userData, संगठन और चाबियाँ हैं consts के साथ मज़ाक उड़ाया डेटा, मैं परीक्षण करना चाहते हैं getUserDetail समारोह, लेकिन जब मैं चलाने के लिए परीक्षण कर रहे हैं कुछ लाइनों है कि पर्दाफाश किया है ।

jest.mock("axios");

jest.mock("./userDetail", () => {
    const originalModule = jest.requireActual("./userDetail");

    //Mock the default export and named export 'foo'
    return {
        __esModule: true,
        ...originalModule,
        default: jest.fn(async () => {
            return {
                user: userData,
                roles: [userData.role],
                orgs: [organization],
                apikeys: apiKeys,
            };
        }),
    };
});

describe("Test userDetail", () => {
    // This is the part that I can't cover with the tests
    it("getUserDetail should return the mocked response", async () => {
        expect.assertions(1);
        const userDetail = await getUserDetail("[email protected]");
        expect(userDetail).toEqual({
            user: userData,
            roles: [userData.role],
            orgs: [organization],
            keys: keys,
        });
    });

    it("getUser should return an user", async () => {
        expect.assertions(1);
        const payload = { data: userData };
        axios.get = jest.fn().mockResolvedValue(payload);
        await expect(getUser("[email protected]")).resolves.toEqual(userData);
    });

    it("getOrganization should return an organization", async () => {
        expect.assertions(1);
        const payload = { data: organization };
        axios.get = jest.fn().mockResolvedValue(payload);
        await expect(getOrganization("[email protected]")).resolves.toEqual(organization);
    });

    it("getKeys should return an array of keys", async () => {
        expect.assertions(1);
        const payload = { data: keys };
        axios.get = jest.fn().mockResolvedValue(payload);
        await expect(
            getKeys({
                items: [organization],
                total: 0,
                page: 0,
                per_page: 0,
            })
        ).resolves.toEqual(keys);
    });
});


javascript jestjs node.js unit-testing
2021-11-22 22:33:54
1

सबसे अच्छा जवाब

1

उन लाइनों नहीं कर रहे हैं कवर किया जा रहा है, क्योंकि आप मजाक कर रहे हैं के कार्यान्वयन के लिए उन्हें

2021-11-23 18:12:27

अपने जवाब के लिए धन्यवाद, लेकिन उन लाइनों एपीआई कॉल के साथ, मैं नहीं करना चाहते हैं कॉल करने के लिए सर्वर के लिए प्रत्येक परीक्षा. कैसे कर सकते हैं मैं उन को कवर लाइनों के बिना बुला एपीआई?
GeeGeeks

नकली axios के बजाय अपने कार्यों
Jacob Waller

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में