Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

514
Vistas
Node v13 / Jest / ES6 — native support for modules without babel or esm

Is it possible to test ES6 Modules with Jest without esm or babel? Since node v13 supports es6 natively have tried:

//package.json
{
  …
  "type": "module"
  …
}



//__tests__/a.js
import Foo from '../src/Foo.js';


$ npx jest

Jest encountered an unexpected token
…
Details:

/home/node/xxx/__tests__/a.js:1
import Foo from '../src/Foo.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module

When babel is added a transpiler, it works, but can es6 modules be used natively as well?

over 4 years ago · Santiago Trujillo
4 Respuestas
Responde la pregunta

0

Yes, it is possible from jest@25.4.0. From this version, there is a native support of esm, so you will not have to transpile your code with babel anymore.

It is not documented yet, but according to this issue you have to do 3 easy steps to achieve that (At the time of writing this answer):

  • Make sure you don't transform away import statements by setting transform: {} in your jest config file
  • Run node@^12.16.0 || >=13.2.0 with --experimental-vm-modules flag
  • Run your test with jest-environment-node or jest-environment-jsdom-sixteen.

So your jest config file should contain at least this:

export default {
    testEnvironment: 'jest-environment-node',
    transform: {}
    ...
};

And to set --experimental-vm-modules flag, you will have to run Jest from package.json as follows (I hope this will change in the future):

"scripts": {
    "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
}

I hope, this answer was helpful to you.

over 4 years ago · Santiago Trujillo Denunciar

0

I followed the tips provided in the accepted answer, but I added the property "type": "module" in my package.json in order to jest works properly. This is what I done:

In package.json:

"devDependencies": {
    "jest": "^26.1.0",
    "jest-environment-jsdom-sixteen": "^1.0.3",
    "jest-environment-node": "^26.1.0"
  },
  "scripts": {
    "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
  },
  "type": "module",
  "jest": {
    "transform": {},
    "testEnvironment": "jest-environment-jsdom-sixteen"
  }
over 4 years ago · Santiago Trujillo Denunciar

0

In addition to @Radovan Kuka's answer, here's how to run Jest with ES modules, using npx:

"test:monitoring": "npx --node-arg=--experimental-vm-modules jest -f monitoring.test.js --detectOpenHandles",

The benefit is that one doesn't need to provide the absolute node_modules path.

over 4 years ago · Santiago Trujillo Denunciar

0

Note that this is is still experimental, but we have documented how to test this, so there's hopefully less confusion.

https://jestjs.io/docs/en/ecmascript-modules

The steps in https://stackoverflow.com/a/61653104/1850276 are correct

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda