jestjs/jest

Support ESM versions of all pluggable modules

Open

#11.167 aberto em 7 de mar. de 2021

Ver no GitHub
 (26 comments) (66 reactions) (0 assignees)TypeScript (6.653 forks)batch import
:rocket: Feature RequestHelp WantedPinned

Métricas do repositório

Stars
 (45.361 stars)
Métricas de merge de PR
 (Mesclagem média 11d 22h) (11 fundiu PRs em 30d)

Description

Once Node 10 is EOL at the end of April, lots of libraries and modules will probably be written in native ESM rather than CJS. While we've mostly been focusing on getting tests running in ESM, we're sorta limited/slowed down by the fact the vm APIs are experimental and flagged on Node's side. That is not the case for "normal" ESM.

We already support user configuration written in ESM.

With that in mind, all modules in Jest that are "pluggable" should be able to load ESM.

  • dependencyExtractor (#12008)
  • globalSetup (#11267)
  • globalTeardown (#11267)
  • preset (#11200)
  • prettierPath
  • reporters (#11427)
  • resolver
  • runner (#11232)
  • snapshotResolver
  • snapshotSerializers
  • testEnvironment (#11033)
  • testResultsProcessor (#12006)
  • testRunner (#11232)
  • testSequencer (#11207)
  • transformers (#11163)
  • watchPlugins (#11315)

Any and all help here would be greatly appreciated!

In general, it means attempting to require, and if that fails with an ESM error, use import() and verify it's the default export. Paired with an integration test that has the module in question written in ESM and verifying it works.

Example: https://github.com/facebook/jest/blob/ab014c140af2e10ae7c5790c2406009790787cdb/packages/jest-transform/src/ScriptTransformer.ts#L177-L196


Whenever we drop Node 10 (probably for Jest 28) we can do just the import call as that can load both ESM and CJS, but that'll be a simple refactor as it's just removing the initial require and try-catch. So I still think it's worth it to add support now as the code difference is quite small and the later refactor is minimal

Guia do colaborador