加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 获取ReferenceError:在mocha中运行react nativ

发布时间:2020-12-15 20:17:01 所属栏目:百科 来源:网络整理
导读:在mocha中运行react-native测试时,我收到以下错误: mocha --require test/init.js --compilers js:test/compiler.js 'src/**/*.spec.js'Initializing tap mocha reporter...1..7ok 1 test1…not ok 7 test7 ReferenceError: fetch is not defined at foo (s
在mocha中运行react-native测试时,我收到以下错误:

> 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.

解决方法

问题是在mocha这样的节点环境中无法获取提取.我不确定为什么react-native-mock(我也在使用)没有模拟它,但解决方案是在初始化我的mocha测试时需要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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读