Skip to content

Instantly share code, notes, and snippets.

@lieunttuit
Forked from rstacruz/README.md
Created March 31, 2021 09:15
Show Gist options
  • Select an option

  • Save lieunttuit/0293f2d4b0a1d4407aad0ecc24c77ed1 to your computer and use it in GitHub Desktop.

Select an option

Save lieunttuit/0293f2d4b0a1d4407aad0ecc24c77ed1 to your computer and use it in GitHub Desktop.
Setting up Jest with ESM

Setting up Jest with ESM

Here are some different ways on how to set up Jest to support ESM. This applies for Jest v25, Node v13, and Babel v7.

Using Babel

Add babel-jest.

yarn add --dev @babel/core @babel/plugin-transform-modules-commonjs babel-jest

Configure Babel. We'll use env.test here so not to interfere with your build process.

// babel.config.js
module.exports = {
  env: {
    test: {
      plugins: ["@babel/plugin-transform-modules-commonjs"]
    }
  }
};

Configure Jest:

// jest.config.js
module.exports = {
  "transform": {
    "^.+\\.[t|j]sx?$": "babel-jest"
  },
};

You're done.

Using jest-esm-transformer

Add jest-esm-transformer - this is a preset configuration of Babel to support ESM transpilation.

yarn add --dev jest-esm-transformer

Configure Jest.

// jest.config.js
module.exports = {
  "transform": {
    "\\.m?jsx?$": "jest-esm-transformer"
  },
};

You're done.

Using esm

As of March 2020, using esm is currently not possible. Follow these threads for details.

Using buble

See buble-jest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment