问题描述
我是打字稿的新手,注意到了我没想到的行为。当我使用 js-cookie
包模拟命名导入时,它模拟了该属性的特定实例,但错误地选择了要使用的正确类型。
import Cookies from "js-cookie"
import { mocked } from "ts-jest/utils"
jest.mock("js-cookie")
const mockedCookies = mocked(Cookies,true)
beforeEach(() => {
mockedCookies.get = jest.fn().mockImplementation(() => "123")
})
it("SampleTest",() => {
//this line throws an error as it is using a difference interface to check against
mockedCookies.get.mockImplementationOnce(() => undefined)
//continue test...
}
在 @types/js-cookies
中有两个定义,当我将其引用为 get()
时,它始终使用 mockCookie.get.<some-jest-function>
版本。因此,我收到打字稿错误说 Type 'undefined' is not assignable to type '{ [key: string]: string; }'.
。
/**
* Read cookie
*/
get(name: string): string | undefined;
/**
* Read all available cookies
*/
get(): {[key: string]: string};
我可以通过每次总是重新声明 jest.fn()
来解决这个问题,但更喜欢使用方便的 jest 函数(如 mockImplementationOnce)。
我做错了吗?有没有办法强制使用哪种 get
类型?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)