Configure Jest to use it with NodeJS ECMAScript Modules

  1. Initialize your application:

      npm init
    
  2. To use ES modules, add type: "module" to package.json file.

  3. Install Jest

     npm i jest
    
  4. Initialize Jest

      npm init jest@latest
    
  5. In jest.config.mjs file: de-comment transform key and assign it a value like transform: { "^.+\\.js$": "babel-jest" }. Just be aware that the rejex will depend on the file extensions on your project.

  6. Install babel

     npm i @babel/core @babel/node @babel/preset-env
    
  7. Configure babel by creating .babelrc file in the root folder.

  8. Inside .babelrc file add:

     {
         "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
     }
    

To start running your tests:

  1. add the test script to scripts inside package.json file

      "scripts": {
         "test": "jest"
       },
    
  2. run the script

     npm run test