使用 React 和 Typescript 非常棒,但有时会导致令人头疼的问题。在这种情况下,我的所有测试都因与 jest-fetch-mock 相关的相同错误而失败:
> NODE_ENV=test jest
FAIL src/store/index.test.tsx
● Test suite failed to run
src/setupTests.ts:6:43 - error TS2352: Conversion of type 'Global & typeof globalThis' to type 'GlobalWithFetchMock' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Property 'fetchMock' is missing in type 'Global & typeof globalThis' but required in type 'GlobalWithFetchMock'.
6 const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/jest-fetch-mock/types/index.d.ts:15:5
15 fetchMock: FetchMock;
~~~~~~~~~
'fetchMock' is declared here.
文件没有变化setupTests.ts
,所以可能是由于某些依赖项的更新引起的。我尝试删除 node_modules 并重新安装,清除缓存并更改节点版本,但没有任何改变。