Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have just started learning
jest
testing and created a sample application to get familiar with
jest
testing. However, I am getting following error...
Language.js
const calculateTip = (total, percentage) => {
return total + ((percentage / 100) * total) + 1;
export {
calculateTip,
package.json
"dependencies": {
"express": "^4.18.2"
"type": "module",
"main": "src/app.js",
"scripts": {
"start": "node src/app.js",
"test": "cls && env-cmd -f ./envs/test.env jest --watchAll"
"devDependencies": {
"env-cmd": "^10.1.0",
"jest": "^29.2.2",
"supertest": "^6.3.1"
I have tried googling for the solution but no luck so far.
Node.js supports esm syntax only from v16, but jest doesn't support it yet.
Therefore, you have 2 choices:
Don't use the esm syntax (import
/ export
)
Add jest-babel with it's config to transpile (convert) esm syntax to cjs one.
–
Jest now ships with experimental support for ECMAScript Modules (ESM):
https://jestjs.io/docs/ecmascript-modules
The simplest way forward is to update your test script to execute node
with --experimental-vm-modules
e.g.
node --experimental-vm-modules node_modules/jest/bin/jest.js
or to add an environment variable to the test script
NODE_OPTIONS=--experimental-vm-modules npx jest
See the link above for more details.
–
You can fix it by installing jest-babel and then creating babel.config.cjs
in your root directory and pasting the following into it:
//babel.config.cjs
module.exports = {
presets: [
'@babel/preset-env',
targets: {
node: 'current',
//package.json
"type": "module"
//tsconfig.json
"target": "esnext",
"module": "commonjs",
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.