使用 Typescript 模拟 fetch 的最简单方法是什么?
我只想做一些简单的事情,如下所示。但 Typescript 告诉我,我与完整获取对象的定义不匹配。
Type 'Mock<Promise<{ json: () => Promise<{ test: number; }>; }>, []>' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
Type 'Promise<{ json: () => Promise<{ test: number; }>; }>' is not assignable to type 'Promise<Response>'.
Type '{ json: () => Promise<{ test: number; }>; }' is missing the following properties from type 'Response': headers, ok, redirected, status, and 11 more.
Run Code Online (Sandbox Code Playgroud)
解决这个问题最简单的解决方案是什么?实际上模拟了整个获取对象或其他解决方案?
global.fetch = jest.fn(() => …
Run Code Online (Sandbox Code Playgroud)