gpt4 book ai didi

javascript - 访问另一个函数中的 jest.fn 函数

转载 作者:行者123 更新时间:2023-12-02 21:52:06 28 4
gpt4 key购买 nike

我在编写 Jest 单元测试用例时遇到以下情况:

elasticsearch.Client = jest.fn().mockImplementation(() => {
return {
update: jest.fn().mockImplementation(() => {
return {}
})
}
});

现在,我想对更新函数调用进行一些预期。如何在测试用例中访问此处的更新功能。

我可以访问elasticsearch.Client及其模拟变量作为elasticsearch.Client.mock。但是我如何才能访问类似的更新功能呢?

最佳答案

您可以尝试将 update 的模拟函数移至外部作用域:

const updateMock = jest.fn().mockImplementation(() => {
return {}
});

elasticsearch.Client = jest.fn().mockImplementation(() => {
return {
update: updateMock
}
});

然后您可以在断言中使用updateMock。例如:

expect(updateMock).toHaveBeenCalled()

关于javascript - 访问另一个函数中的 jest.fn 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60108400/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com