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

181
Visualizações
Object destructuring assignment works with Node but not with Jest

'Using the Playwright Library [1.21.0] with Jest [27.5.1] to run browser automation tests on Node.js [14.18.2].

ESM is the current module format.

// package.json
...
"type": "module",
...

I setup a quick test with the following code:

// playwright-hello.js

import {chromium} from 'playwright';

async function main() {
  const browser = await chromium.launch();

  const page = await browser.newPage();

  await page.goto('http://www.example.com');
  await page.screenshot({ path: 'screenshot.png' });

  await browser.close();
}

main().catch(console.error);

The above code works via CLI command node playwright-hello with no issues.

However, when I try to run similar code with Jest, I get the following error:

 FAIL  test/index.test.js
  ● Test suite failed to run

    SyntaxError: The requested module 'playwright' does not provide an export named 'chromium'

      at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:779:5)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

I fixed this by updating the test code to use the default export instead:

// changed to use default export instead of named one
import playwright from 'playwright';

// prepended `playwright.` to initial `chromium.launch()`
const browser = await playwright.chromium.launch();

How come the object destructuring assignment in playwright-hello.js [i.e. import {chromium}...] works with node.js CLI, but not with Jest? In other words, node.js did not complain about playwright library not providing a named export, why?

Note: I'm running Jest as follows for ES6 modules support:

NODE_OPTIONS='--experimental-vm-modules' npx jest ./test/index
about 4 years ago · Juan Pablo Isaza
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