获取 ReferenceError:在 mocha 中运行 React Native 测试时未定义 fetch

2024-05-01

在摩卡中运行反应本机测试时,出现以下错误:

> mocha --require test/init.js --compilers js:test/compiler.js 'src/**/*.spec.js'

Initializing tap mocha reporter...
1..7
ok 1 test1
…
not ok 7 test7
  ReferenceError: fetch is not defined
      at foo (src/foo.js:59:8)
      at Context.<anonymous> (src/__specs__/foo.spec.js:9:30)
# tests 7
# pass 6
# fail 1
npm ERR! Test failed.  See above for more details.

问题是 fetch 在像 mocha 这样的节点环境中不可用。我不确定为什么react-native-mock(我也在使用)没有模拟,但解决方案是要求isomorphic-fetch初始化我的摩卡测试时。

具体来说,如果您还没有 init 文件,请将其添加到您的 mocha 命令行中:

> mocha --require init.js …

在 init.js 中,需要 isomorphic-fetch:

require('isomorphic-fetch')

然后重新运行 mocha:

> mocha --require test/init.js --compilers js:test/compiler.js 'src/**/*.spec.js' && standard

Initializing tap mocha reporter...
1..7
ok 1 test1
…
ok 7 test7
# tests 7
# pass 7
# fail 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取 ReferenceError:在 mocha 中运行 React Native 测试时未定义 fetch 的相关文章

随机推荐