Next + Jest does not support common .babelrc
See original GitHub issueI recently tried to define special presets in .babelrc
only for test environment in with-jest example.
When importing a file for testing that uses React and/or StyledJSX and that does not manually import
React and/or StyledJSX, the test will fail with following error message:
SyntaxError: Unexpected token import
This is because the automatically added imports of React/StyledJSX are always es2015 style module imports.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior
A simple .babelrc
that only defines a special test
env should be possible.
In my opinion it should be possible to use the following .babelrc
:
{
"presets": "next/babel",
"env": {
"test": {
"presets": [
"env",
"next/babel"
]
}
}
}
I played with this issue on a separate branch. Currently you could work around this issue by importing React & StyledJSX manually:
import React from 'react'
import _JSXStyle from 'styled-jsx/style'
Current Behavior
Either you define development
, production
and test
envs in .babelrc
whereas development
and production
or you manually import React / StyledJSX. Otherwise tests with JEST will fail.
Steps to Reproduce (for bugs)
- Goto
examples/with-jest
- Simplify your
.babelrc
as proposed - Run
$ npm run test
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Thank you for your quick response @arunoda . Unfortunately those suggestions won’t fix the issue. I added your suggestions to
.babelrc
in https://github.com/pex/next.js/commit/220d7a8e30cb244524f942434197725a0a5aa75d and tests fail with the same message they did before.Another note on this:
next/babel
andenv
is;["env", { "modules": "commonjs" }]
is obsolete, though. Since this is default inenv
: https://babeljs.io/docs/plugins/preset-env/#optionsmodules@sergiodxa I think the issue is,
["env", { "modules": "commonjs" }]
should run beforenext/babel
in test.If you set a default one. It’ll run always at first.