Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spread operator in React Native causes 'Unexpected token' error with mocha

I'm starting a React Native project, and I'd like to use ECMAScript 2015 for my code, and mocha for my unit tests. I've installed babel-register, babel-preset-es2015, and babel-preset-stage-2, and added this to my project's package.json file:

"babel": {
    "presets": ["es2015", "stage-2"]
}

But when I run mocha --compilers js:babel-register and try to test a module that imports a React Native API...

import React from 'react-native';

let {Dimensions} = React;

...I get an error about the spread operator used by React:

node_modules/react-native/Libraries/react-native/react-native.js:107
...require('React'),
^^^

SyntaxError: Unexpected token ...

Is this a babel bug—shouldn't the stage-2 preset add support for the spread operator? Or am I missing something else?

like image 397
Rob Johansen Avatar asked Jan 01 '16 20:01

Rob Johansen


1 Answers

I just started seeing this too. Seems like a bug. AFAIK babel doesn't compile files under node_modules because they should always be precompiled.

Also this is a pretty recent addition

https://github.com/facebook/react-native/commit/f9b744d50137de25357994fe2e829f98104e2242

like image 136
Epeli Avatar answered Oct 07 '22 14:10

Epeli