错误:无法读取属性提取.使用 jest-expo 和 react native

2022-01-23 00:00:00 reactjs javascript react-native expo jestjs

TypeError:无法读取未定义的获取"属性

TypeError: Cannot read property 'fetch' of undefined

  at node_modules/whatwg-fetch/fetch.js:4:8
  at Object.<anonymous> (node_modules/whatwg-fetch/fetch.js:466:3)
  at Object.<anonymous> (node_modules/jest-expo/src/setup.js:97:348)

使用 jest-expo 运行测试时出错.如果有人有解决方案,请分享.

Got error while running tests with jest-expo. If anyone have solution please share.

推荐答案

显然这是没有代码片段的猜测,但是看当时 fetch.js 的样子,undefined 值,其 fetch 属性是变量 self (参见第 1 行),它被设置为 this 上下文(参见第 466 行).

Obviously this is a guess without a code snippet, but looking at what fetch.js looked like at the time, the undefined value whose fetch property we can't read is the variable self (see line 1), which was meant to be set to the this context (see line 466).

因此,似乎 fetch.js 期望在 this 引用 window 的环境中运行,但 jest-expo,作为一个测试工具,正在其他一些环境中运行它,可能是在 thisundefined 的严格模式下.

Therefore, it seems that fetch.js is expecting to run in an environment where this refers to the window, but jest-expo, being a testing tool, is running it in some other environment, probably in strict mode where this is undefined.

相关文章