Configure Jest to use it with NodeJS ECMAScript Modules
Initialize your application:
npm init
To use ES modules, add
type: "module"
to package.json file.Install Jest
npm i jest
Initialize Jest
npm init jest@latest
In jest.config.mjs file: de-comment
transform
key and assign it a value liketransform: { "^.+\\.js$": "babel-jest" }
. Just be aware that the rejex will depend on the file extensions on your project.Install babel
npm i @babel/core @babel/node @babel/preset-env
Configure babel by creating .babelrc file in the root folder.
Inside .babelrc file add:
{ "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]] }
To start running your tests:
add the
test
script toscripts
inside package.json file"scripts": { "test": "jest" },
run the script
npm run test