Jestjs 笑话:检查模拟模块函数被调用了多少次

Jestjs 笑话:检查模拟模块函数被调用了多少次,jestjs,Jestjs,我在代码中使用模块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)

我在代码中使用模块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

您创建的手动模拟根本不是一个,而是一个(即替代实现)

你甚至不需要它。您可以删除手动模拟并按如下方式编写测试:

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);
});