Jest tests won't work without babel
See original GitHub issueVersion
3.0.0-rc.10
Node and OS info
Node 10.7.0 / npm 6.3.0-next.0 / Windows 10
Steps to reproduce
- Create new project with only Jest plugin selected
- Run
npm run test:unit
What is expected?
PASS tests/unit/HelloWorld.spec.js
HelloWorld.vue
√ renders props.msg when passed (22ms)
What is actually happening?
FAIL tests/unit/HelloWorld.spec.js
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Details:
C:\node_workspace\test\tests\unit\HelloWorld.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { shallowMount } from '@vue/test-utils';
^
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
It works as expected if I install both Babel and Jest.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Node v13 / Jest / ES6 — native support for modules without ...
I tried this but it doesn't really work very well. The jest name isn't available within the test module. I'm sure there are...
Read more >Troubleshooting - Jest
Troubleshooting. Uh oh, something went wrong? Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why.
Read more >Testing ES6 Code with Jest and Babel - Documentation
By default JavaScript unit tests use Mocha and only work with es5 code. This tutorial covers how to create custom unit tests with...
Read more >babel-jest - npm
Jest plugin to use babel for transformation.. Latest version: 29.3.1, last published: 2 months ago. Start using babel-jest in your project ...
Read more >Simple React Unit Test with Jest and Babel | by Vidisha Pal
In this section, we will look at how to configure Jest and Testing library to ... Open in app ... When we write...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Many thanks @Mourdraug!
This finally solved my Jest + ES6 without babel problems (for an unrelated project), I’ll add my simplified solution to this.
I eventually understood by staring at commit 65d5d36d from the jestPlugin.spec.js, this adds the babel and a babel plugin as dependencies, not as devDependencies.
I have a completely unrelated project. But as is pointed out here Jest uses node so Jest needs to use babel even if your project doesn’t. So for a project which hasn’t installed babel yet, you still need to install babel, but as a regular dependency:
Then I modified my
package.json
to include:Now
npm test
runs without theimport
error. Here I don’t need a.babelrc
file.My motivation for opting out babel is actually quite simple. I’d like to use ES6 without transpiling code to ES5 I don’t really use babel tho, so I’m not sure if it’s possible to configure it to handle imports and leave rest as is.