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.

Next + Jest does not support common .babelrc

See original GitHub issue

I 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)

  1. Goto examples/with-jest
  2. Simplify your .babelrc as proposed
  3. Run $ npm run test

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pexcommented, Jul 8, 2017

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:

1reaction
arunodacommented, Jul 8, 2017

@sergiodxa I think the issue is, ["env", { "modules": "commonjs" }] should run before next/babel in test.

If you set a default one. It’ll run always at first.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next + Jest does not support common .babelrc #2499 - GitHub
I recently tried to define special presets in .babelrc only for test environment in with-jest example. When importing a file for testing ...
Read more >
Configuring Jest for Next.JS (React) and Babel from scratch
Construct your Jest, Babel and package. json configuration files for a working Next. JS environment. Jest, library for testing Javascript.
Read more >
Jest and Babel transpilation - SyntaxError: Cannot use import ...
So what seems to happen to me is that the pRetry is not transformed from it's ESModule Code (the source of pRetry starts...
Read more >
Using with webpack - Jest
Non-Babel JavaScript transformations can be handled with Jest's transform config option.
Read more >
Advanced Features: Next.js Compiler
We're working to port as many commonly used Babel transformations as possible, as well as supporting plugins in the future. Guides: Building FormsTurbopack....
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