Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
How to use ESM tests with jest?

My setup

  • Node v14.16.0
  • I have one test file that contains a simple test written with ESM modules (import).
  • In package.json I have a script (following Jest documentation adapted to Windows): "test": "set \"NODE_OPTIONS=--experimental-vm-modules\" && npx jest"
  • My tests are run w/ npm run test (the script above)

My goal: Run the ESM tests using jest.

My attempts:

(1) CommonJS App w/ .js test

  • package.json contains "type": "commonjs"
  • Test file named test.js
  • Error: SyntaxError: Cannot use import statement outside a module (This is expected because the test is being run as a commonjs file.)

(2) CommonJS App w/ .mjs test

  • package.json contains "type": "commonjs"
  • Test file named test.mjs
  • Error: No tests found (Jest isn't finding the file?)

(3) ESM App w/ .js or .mjs

  • package.json now has "type": "module"
  • Test file named test.mjs or test.js
  • Error: ReferenceError: module is not defined at ...jest.config.js:6:1

(4) CommonJS App and CommonJS test

  • package.json contains "type": "commonjs"
  • Test file named test.js but using the commonjs require syntax (and without the ESM modules I'd like to include); Note that this is not what I want, but included just to show there's nothing wrong with the test itself.
  • Test runs fine and passes
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Here are the steps I took to run Jest with a test using ESM. The source files under test were also written using ESM.

  1. Set my node version to 14.16.0
  2. npm i jest -D installs Jest as a dev dependency
  3. Add "type": "module" to package.json
  4. Update scripts in package.json to include "test": "node --experimental-vm-modules ./node_modules/.bin/jest"
  5. Create a jest.config.js file with the following content: export default { transform: {} }
  6. Have at least one test that could be found using the default testMatch (mine was inside __tests__ dir)
  7. npm test

There is a more complete example in the webpack-strip-debug-loader repository on GitHub.

over 4 years ago · Santiago Trujillo Relatório

0

After a lot of fiddling, this is my successful setup (the parts that matter):

  • package.json has
  "type": "commonjs",
  "script": {
    "test": "NODE_OPTIONS=--experimental-vm-modules jest"
  }
  • jest.config.mjs (yes, esModule)
export default {
  moduleFileExtensions: [
    "mjs",
    // must include "js" to pass validation https://github.com/facebook/jest/issues/12116
    "js",
  ],
  testRegex: `test\.mjs$`,
};

That having to include "js" is a historic relic from the times modules where obviously .js files. Follow https://github.com/facebook/jest/issues/12116 for update.

over 4 years ago · Santiago Trujillo Relatório

0

For me to work it was only necessarry to set script "test" as follows:

"test": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest"

and yeah, don't forget -> npm i cross-env jest

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda