React-dates: Error when running tests with imported react-dates

Created on 7 Aug 2018  路  3Comments  路  Source: airbnb/react-dates

I'm getting an error saying
(function (exports, require, module, __filename, __dirname) { export const DISPLAY_FORMAT = 'L'; SyntaxError: Unexpected token export when I run my tests with this command: NODE_ENV=test mocha --compilers js:babel-core/register --require ./react_redux/test/test_helper.js './react_redux/*/*/test*/*.@(js|jsx)'.

This seems to be a problem with the react-dates package as this is the only time I am getting this error.

Using Mocha, Enzyme and Chai for testing.

I have this in my package.json:

"babel": {
  "presets": [
    "es2015",
    "react"
  ]
}

and this in my .babelrc:

{
  "presets": ["es2015", "react"],
  "plugins": ["transform-object-rest-spread"]
}

Some relevant versions:

"react": "^16.2.0",
"react-dates": "^17.1.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"chai": "^3.5.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"mocha": "^5.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",

Somehow I managed to get passed this error by using relative paths (e.g. '../../../../../node_modules/react-dates/constants') but this doesn't seem like the right approach, and also just lead me down a rabbit hole of other errors, mostly within node_modules.

The component renders fine without any errors, this issue is only when running tests.

Most helpful comment

Finally found the issue - In another file I was importing react-dates/src/constants _(note the src)_ which was importing https://unpkg.com/[email protected]/src/constants.js which is using the export syntax. Didn't realise until your comment @ljharb. Thanks!

All 3 comments

@ljharb are the "es2015" and "react" babel presets sufficient here? Do you also need to include some sort of JS shims when running tests?

No presets should be required, since babel shouldn't be touching node_modules code, and all our code should be transpiled prepublish.

You can see that that path is https://unpkg.com/[email protected]/constants.js, which requires https://unpkg.com/[email protected]/lib/constants.js, which does not use export syntax.

What's in test_helper.js?

Finally found the issue - In another file I was importing react-dates/src/constants _(note the src)_ which was importing https://unpkg.com/[email protected]/src/constants.js which is using the export syntax. Didn't realise until your comment @ljharb. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ekohanyi picture ekohanyi  路  3Comments

Daniel15 picture Daniel15  路  3Comments

maciej-w picture maciej-w  路  3Comments

swaritkohli picture swaritkohli  路  3Comments

jpollard-cs picture jpollard-cs  路  3Comments