Skip to content

How to run jest when using es6 packages in node_modules #5241

@nerfologist

Description

@nerfologist

Is this a bug report?

Not sure. More of a configuration issue I guess.

TLDR

Trying out the feature described in #1125 which should allow the usage of libraries distributed as es6 modules in an app create with create-react-app, the app runs fine but the Jest test suite will not.

How to reproduce

  • create a new create-react-app application npx create-react-app jest-es6-modules
  • in the new application, yarn add lodash-es
  • modify src/App.js adding any named import, such as import { isEmpty } from 'lodash-es'
  • run yarn start (everything works)
  • run yarn test

Here's the Jest error, related to Jest not babel-transforming the modules in es6 format:

  ● 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.

    [...]

    Details:

    /Users/nerfologist/test/jest-es6-modules/node_modules/lodash-es/lodash.js:10
    export { default as add } from './add.js';
    ^^^^^^

    SyntaxError: Unexpected token export

      1 | import React, {Component} from 'react';
      2 | import logo from './logo.svg';
    > 3 | import {isEmpty} from 'lodash-es';
        | ^
      4 | import './App.css';
      5 |
      6 | class App extends Component {

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (src/App.js:3:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.279s
Ran all test suites related to changed files.

Question

What is the best/recommended way to have Jest transform one or more of these node_modules?

I'm thinking of adding a Jest configuration containing the transformIgnorePatterns directive.

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions