How to set up Jest for ESM

Node v10 is coming to end of life in the upcoming April 30. And the node js community is starting to move all their projects and modules to ESM (ES6 Modules) ditching commonjs as a good practice.

Today I am going to do a quick tutorial on how to set u…


This content originally appeared on DEV Community and was authored by Rocktim Saikia

Node v10 is coming to end of life in the upcoming April 30. And the node js community is starting to move all their projects and modules to ESM (ES6 Modules) ditching commonjs as a good practice.

Today I am going to do a quick tutorial on how to set up jest for testing your ES6 modules with minimum set up and requirements. So let's get started.

It is important to mention that Jest does have built-in ESM support but it's not very stable and I often run into various issues while using it. So the method I am writing here is the best solution that has worked for me so far.

First, let's install the required packages.

npm install babel-jest @babel/plugin-transform-modules-commonjs —dev

I am assuming that you already have the core jest package installed, If you don't, then install it by running

npm install jest —dev

Now we have to configure jest to use the babel-jest plugin. You can create a separate jest.config.js file but I am going to put the configuration right in the package.json since it is very simple. Add the following lines to your package.json.

// package.json

"jest": {
    "transform" : {
        "\\.[jt]s?$": "babel-jest"
    }
}

After setting the jest config, create a .babelrc file in the root of the project directory. And add the following line

//.babelrc

{
    "plugins": ["@babel/plugin-transform-modules-commonjs"]
}

And that’s it. Now we can test ES6 modules running Jest.

Here is a simple boilerplate that uses this trick.
https://github.com/RocktimSaikia/node-boilerplate ?

This post was originally posted on my website.


This content originally appeared on DEV Community and was authored by Rocktim Saikia


Print Share Comment Cite Upload Translate Updates
APA

Rocktim Saikia | Sciencx (2021-04-25T13:08:49+00:00) How to set up Jest for ESM. Retrieved from https://www.scien.cx/2021/04/25/how-to-set-up-jest-for-esm/

MLA
" » How to set up Jest for ESM." Rocktim Saikia | Sciencx - Sunday April 25, 2021, https://www.scien.cx/2021/04/25/how-to-set-up-jest-for-esm/
HARVARD
Rocktim Saikia | Sciencx Sunday April 25, 2021 » How to set up Jest for ESM., viewed ,<https://www.scien.cx/2021/04/25/how-to-set-up-jest-for-esm/>
VANCOUVER
Rocktim Saikia | Sciencx - » How to set up Jest for ESM. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/25/how-to-set-up-jest-for-esm/
CHICAGO
" » How to set up Jest for ESM." Rocktim Saikia | Sciencx - Accessed . https://www.scien.cx/2021/04/25/how-to-set-up-jest-for-esm/
IEEE
" » How to set up Jest for ESM." Rocktim Saikia | Sciencx [Online]. Available: https://www.scien.cx/2021/04/25/how-to-set-up-jest-for-esm/. [Accessed: ]
rf:citation
» How to set up Jest for ESM | Rocktim Saikia | Sciencx | https://www.scien.cx/2021/04/25/how-to-set-up-jest-for-esm/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.