【问题标题】:Jest test group of async functions with Promise.all用 Promise.all 开玩笑测试异步函数组
【发布时间】:2022-01-01 11:47:47
【问题描述】:

我正在尝试测试这些功能,但有几行我无法涵盖,

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、organization 和 keys 是带有模拟数据的 const,我想测试 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("test@test.com");
        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("test@test.com")).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("test@test.com")).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 node.js unit-testing jestjs


    【解决方案1】:

    这些行没有被覆盖,因为你在模拟它们的实现

    【讨论】:

    • 感谢您的回答,但这些行是 API 调用,我不想为每个测试调用服务器。如何在不调用 API 的情况下覆盖这些行?
    • 模拟 axios 而不是你的函数
    猜你喜欢
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    相关资源
    最近更新 更多