-1

I would like to run the following test with Jest. I have no babel-config or jest-config yet. How can I configure jest to use ES6 features like import/export?

index.js:

export const add = (a, b) => a + b;

index.test.js

import { add } from '.';

describe('add', () => {
  it('adds a + b', () => {
    expect(add(1, 2)).toBe(3);
  });
});
yN.
  • 1,847
  • 5
  • 30
  • 47

1 Answers1

0

Turns out it works just like described in the docs at: https://jestjs.io/docs/en/getting-started#using-babel

To use Babel, install required dependencies via yarn:

yarn add --dev babel-jest @babel/core @babel/preset-env

Configure Babel to target your current version of Node by creating a babel.config.js file in the root of your project:

// babel.config.js
module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current',
        },
      },
    ],
  ],
};

Note: Jest needs to have the same major version as the babel-jest package. I had Jest v23 and babel-jest v24 which lead to misguiding error messages.

yN.
  • 1,847
  • 5
  • 30
  • 47