2

我已经尝试按照此处关于使用 AVA 对 js 代码进行单元测试的简单设置进行操作,但我做错了,因为似乎没有考虑到设置。例外:

 ReferenceError: __DEV__ is not defined
at Object.<anonymous> (D:\Vs\app\node_modules\react-native\Libraries\react-native\react-native.js:15:5)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)

我的 RN 项目的根目录中有一个测试文件夹。

我的 _Setup.js 文件如下所示:

import mockery from 'mockery'

// inject __DEV__ 
global.__DEV__ = true
__DEV__ = true
// We enable mockery and leave it on.
mockery.enable()

// Silence mockery's warnings as we'll opt-in to mocks instead
mockery.warnOnUnregistered(false)

我的 package.json 相关部分看起来像

  "ava": {
    "babel": "inherit",
    "files": [
      "tests/**/*.js"
    ],
   "require": [
     "babel-register",
     "babel-polyfill",
     "react-native-mock/mock"
   ]
 },
 "devDependencies": {
   "ava": "^0.15.2",
   "babel-polyfill": "^6.9.1",
   "babel-register": "^6.9.0",
   "enzyme": "^2.4.1",
   "mockery": "^1.7.0",
   "nyc": "^7.0.0",
   "react-addons-test-utils": "^15.2.1",
   "react-dom": "^15.2.1",
   "react-native-mock": "^0.2.4"
 }

测试文件:

import test from 'ava'
import smth from '../app/components/LoadingSpinner'

test('returns 1', t => {
  t.is(1, smth())
})

.babelrc 只有

{ 
   "presets" : ["react-native"]
}

任何提示表示赞赏!感谢:D!

4

1 回答 1

1

一旦我将节点更新到 6.xx,整个问题就解决了

现在文章中提到了这方面:)

于 2016-07-15T13:18:43.730 回答