Javascript React和Typescript-由于类型错误,测试失败;类型转换';全球及;globalThis'的类型;输入';GlobalWithFetchMock'&引用;
使用React和Typescript非常棒,但有时可能会导致令人头痛的问题。在本例中,我的所有测试均失败,并出现与jest fetch mock相关的相同错误:Javascript React和Typescript-由于类型错误,测试失败;类型转换';全球及;globalThis'的类型;输入';GlobalWithFetchMock'&引用;,javascript,reactjs,typescript,unit-testing,jest-fetch-mock,Javascript,Reactjs,Typescript,Unit Testing,Jest Fetch Mock,使用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 g
> 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
文件中没有任何更改,因此可能是由于某些依赖项的更新引起的。
我尝试删除节点模块并重新安装,清除缓存并更改节点版本,但没有任何更改。如果您在
setUpTests中声明GlobalWithFetchMock
为全局,ts
和Typescript仍在抱怨,请尝试将全局类型括在括号中“()”,并最初设置为未知
修改
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock
到
如果您在setUpTests中声明GlobalWithFetchMock
为全局,您应该能够再次成功运行测试,并显示大量绿色:)。ts
和Typescript仍在抱怨,请尝试将全局类型括在括号中“()”,并最初设置为unknown
修改
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock
到
您应该能够再次成功运行测试,并显示大量绿色:)