我也遇到了这个问题(jest vs. typescript-eslint)。 This 是有问题的 eslint 规则。
我尝试了许多解决方案(围绕绑定模拟函数),虽然我仍然愿意找到一种优雅的方法来使 linting 规则静音而不会使我的测试的可读性显着降低,但我决定为我的测试禁用该规则.
就我而言,我正在模拟电子 ipcRenderer 函数:
import { ipcRenderer } from 'electron';
jest.mock('electron', () => ({
ipcRenderer: {
once: jest.fn(),
send: jest.fn(),
removeAllListeners: jest.fn(),
},
}));
然后在测试中,期待调用发送模拟:
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
直接绑定函数,例如
expect(ipcRenderer.send.bind(ipcRenderer)).toHaveBeenCalledTimes(1);
...通过了 eslint 规则,但 jest 不喜欢它:
expect(received).toHaveBeenCalledTimes(expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function bound mockConstructor]