我正在使用jest来模拟具有此签名的方法的实现
(arg1: string, arg2: string, arg3: callback)
(arg1: string, arg3: callback)
我想通过mockImplementation在我的测试中直接调用arg3
func.mockImplementation((arg1: string, arg2: string, arg3: callback) => {
const actualCallback = arg2 // we know we are using the second case here.
actualCallback()
})
但打字稿给了我一个错误。在模拟函数签名时如何跳过打字稿检查?
您可以通过两种方式之一来完成。
func.mockImplementation((arg1: string, arg2: string | callback, arg3: callback) => {
const actualCallback = arg2 as callback // Explicitly tell TS what type it is.
actualCallback()
})
func.mockImplementation((arg1: string, arg2: string, arg3: callback) => {
const actualCallback = arg2
// @ts-ignore
actualCallback()
})