gpt4 book ai didi

jestjs - 开 Jest : check how many times a mocked module function was called

转载 作者:行者123 更新时间:2023-12-02 08:10:07 25 4
gpt4 key购买 nike

我在代码中使用模块 waait 来允许我执行以下操作:

import * as wait from 'waait';
await wait(500);

我创建了一个手动模拟:

module.exports = (() => {
return Promise.resolve();
});

然后我想在我的测试中有这样的断言:

import * as wait from 'waait';
expect(wait).toHaveBeenCalledTimes(1);
expect(wait).toHaveBeenLastCalledWith(1000);

当我运行它时,我得到:

expect(jest.fn())[.not].toHaveBeenCalledTimes()

jest.fn() value must be a mock function or spy.
Received: undefined

最佳答案

您创建的手动模拟不是 mock除了 fake (即替代实现)。

你甚至不需要它。您可以删除手动模拟并像这样编写测试:

import * as wait from 'waait';

jest.mock('waait');
wait.mockResolvedValue(undefined);

it('does something', () => {
// run the tested code here
// ...

// check the results against the expectations
expect(wait).toHaveBeenCalledTimes(1);
expect(wait).toHaveBeenLastCalledWith(1000);
});

关于jestjs - 开 Jest : check how many times a mocked module function was called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52885551/

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