Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test config for AVA + React-Native

I try to test my react-native application using AVA and the babel-preset-react-native

My config looks like this:

"scripts": {
  "test": "ava"
},
"ava": {
  "files": [
    "src/**/__tests__/*.js"
  ],
  "failFast": true,
  "require": [
    "react-native-mock/mock.js",
    "babel-register"
  ],
  "babel": {
    "presets": [
      "react-native"
    ]
  }
},
"devDependencies": {
  "ava": "^0.13.0",
  "babel-preset-react-native": "^1.2.4",
  "babel-register": "~6.4.3",
  "react-native-mock": "0.0.6"
}

…and fails like this:

/Users/zoon/Projets/xxxxx/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/index.js:556
      throw err;
      ^

SyntaxError: /Users/zoon/Projets/xxxxx/src/reducers/env.js: Unexpected token (12:8)
  10 |     case types.RECEIVE_CHANGE_ENV:
  11 |       return {
> 12 |         ...state,
     |         ^
  13 |         current: Environments[action.env]
  14 |       };
  15 |     default:

If I export this babel config in a .babelrc file and use "babel": "inherit" in my AVA config, it fails in an other way:

/Users/zoon/Projets/xxxxx/node_modules/lodash-es/lodash.js:10
export { default as add } from './add';
^^^^^^

SyntaxError: Unexpected token export

I can't understand how to correctly configure this. I've tried Mocha, encountered the same problems.

like image 362
zoontek Avatar asked Oct 18 '22 13:10

zoontek


1 Answers

babel-register ignores node_modules by default. You can set ignore:false in your .babelrc to disable that behavior. You could (probably should) also specify a more limited regular expression to avoid processing everything in node_modules.

See the docs: https://babeljs.io/docs/usage/require/

Your complete config should probably look something like this:


.babelrc

{
  "presets": ["react-native"],
  "ignore": false
}

package.json

{
  "ava": {
    "babel": "inherit"
  }
}
like image 127
James Talmage Avatar answered Oct 21 '22 05:10

James Talmage