question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest tests won't work without babel

See original GitHub issue

Version

3.0.0-rc.10

Node and OS info

Node 10.7.0 / npm 6.3.0-next.0 / Windows 10

Steps to reproduce

  1. Create new project with only Jest plugin selected
  2. 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:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

33reactions
ianchanningcommented, Dec 25, 2018

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:

npm install babel-jest babel-plugin-transform-es2015-modules-commonjs

Then I modified my package.json to include:

  "babel": {
    "plugins": ["transform-es2015-modules-commonjs"]
  },

Now npm test runs without the import error. Here I don’t need a .babelrc file.

7reactions
Mourdraugcommented, Aug 2, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found