类型错误:无法分配给对象“[object global]”的只读属性“fetch”

问题描述 投票:0回答:1

我的测试遇到了一个神秘的错误。

背景:

我有很多测试文件,适合除我以外的所有人。 在唯一失败的测试中,唯一的问题是这一行:

const spyOnFetch = jest.spyOn(global, 'fetch');

我不知道为什么,但我有这个错误:

 FAIL   Native  ...learner-native-app/src/sections/settings/pages/DeleteAccount/DeleteAccount.test.tsx
  ● Test suite failed to run

    TypeError: Cannot assign to read only property 'fetch' of object '[object global]'

      at Object.restore (node_modules/jest-mock/build/index.js:786:35)
          at Set.forEach (<anonymous>)

(node:34885) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

你们有什么想法吗?

我试过了:

  • 使用 nvm 更改我的节点版本
  • 删除自制节点版本
  • 更新npm版本
  • 重新克隆项目
  • 更新纱线

信息:

  • 节点版本:21.7.2

  • NPM 版本:10.5.1

  • 纱线版本:3.8.1

  • 笑话版本:29.5.0

reactjs react-native testing jestjs react-testing-library
1个回答
0
投票

解决方案:

Node 版本与我的测试不兼容。 我用的

20.12
21.7
不兼容。

© www.soinside.com 2019 - 2024. All rights reserved.