相关疑难解决方法(0)

使用 Typescript 和 Jest 进行简单的获取模拟

使用 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)

fetch typescript jestjs

37
推荐指数
3
解决办法
4万
查看次数

标签 统计

fetch ×1

jestjs ×1

typescript ×1