Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest - SyntaxError: Cannot use import statement outside a module

I am using jest:24.9.0 without any configuration, installed globally from a create-react-app. Inside these files I am using es6 modules. There is no error when using "test": "react-scripts test"

However when I move to use jest with "test": "jest --config jest.config.js", I see the below error.

 FAIL  src/configuration/notifications.test.js
  ● Test suite failed to run

    /var/www/management/node/src/configuration/notifications.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import notifications from './notifications';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module
like image 641
Jamie Hutber Avatar asked Jan 23 '20 13:01

Jamie Hutber


People also ask

How do you fix SyntaxError Cannot use import statement outside a module?

The "SyntaxError: Cannot use import statement outside a module" occurs when we use the ES6 Modules syntax in a script that was not loaded as a module. To solve the error, set the type attribute to module when loading a script, or in your package. json for Node.

How to resolve Cannot use import statement outside a module in jest?

The TypeScript jest error "Cannot use import statement outside module" occurs when jest is misconfigured in a TypeScript project. To solve the error, you have to make sure that your TypeScript files are compiled to JavaScript before they are run because jest on its own cannot understand TypeScript.

What does Babel jest do?

Jest ships with one transformer out of the box – babel-jest . It will load your project's Babel configuration and transform any file matching the /\.

Where do you put transformIgnorePatterns?

You can add the transformIgnorePatterns in your root jest. preset. js or your individual jest config files.


3 Answers

Jest doesn't support ES6 module and hence throwing this error when you directly run the test with Jest. if you want to run like that then you have to add babel.

On the other side, when you run the test with react-scripts it uses babel behind the scene to Transpile the code.

In newer version of jest, babel-jest is now automatically loaded by Jest and fully integrated

Hope this answer your question.

Adding babel in jest.

Installation

babel-jest is now automatically loaded by Jest and fully integrated. This step is only required if you are using babel-jest to transform TypeScript files.

npm install --save-dev babel-jest

Usage

In your package.json file make the following changes:

{
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    }
  }
}

Create .babelrc configuration file

Create a babel.config.json config in your project root and enable some presets.

To start, you can use the env preset, which enables transforms for ES2015+

npm install @babel/preset-env --save-dev

In order to enable the preset you have to define it in your babel.config.json file, like this:

{
  "presets": ["@babel/preset-env"]
}

Check for more details on Babel official site

like image 141
Sohail Ashraf Avatar answered Oct 29 '22 01:10

Sohail Ashraf


Since jest is not working with esmodules well, you need to add these configurations in jest.config.js to tell Jest to use commonJS builds instead

moduleNameMapper : {
        '^react-dnd$': 'react-dnd/dist/cjs',
        '^react-dnd-html5-backend$': 'react-dnd-html5-backend/dist/cjs',
        '^dnd-core$': 'dnd-core/dist/cjs',
}
like image 32
snowyBunny Avatar answered Oct 29 '22 02:10

snowyBunny


As answers above have described, Jest doesn't support ES6 modules. So, we need transform the code to a supported module type. We could use babel-jest or ts-jest. I recommend using ts-jest for simplicity.

Step 1: installation:

npm i -D ts-jest @types/jest or yarn add --dev ts-jest @types/jest

Step 2: Configuration

Option 1: create a jest configration file jest.config.js

module.exports = {
    transform: {
        '^.+\\.(ts|tsx)$': 'ts-jest',
    },
};

Option 2: add jest configration into package.json

"jest": {
    "transform": {
        "^.+\\.(ts|tsx)$": "ts-jest"
    }
}

Step 3: Execute your script. Everything should be all set now.

like image 5
paulwen Avatar answered Oct 29 '22 01:10

paulwen