Search code examples
javascriptunit-testingreact-nativeava

React Native unit testing with ava


I've tried following the simple setup from here regarding unit testing js code with AVA, but I am doing something wrong because the setup doesn't seem to be taken into consideration. Exception:

 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)

I have a tests folder in the root of my RN project.

My _Setup.js file looks like this:

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)

My relevant part of package.json looks like

  "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"
 }

Test file:

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

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

.babelrc only has

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

Any hints are appreciated! Thanks :D!


Solution

  • The whole problem was solved once I updated my node to 6.x.x

    This aspect is now mentioned in the article :)