开玩笑试验组异功能的承诺。所有

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;

这是我的测试,用户数据、组织和键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

谢谢你的回答,但是这些行API电话,我不想打电话的服务器对每一个测试。 我怎么能复盖这些行,而不调用API?
GeeGeeks

模拟 axios 而不是你的功能
Jacob Waller

其他语言

此页面有其他语言版本

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