Skip to content

Support ESM versions of all pluggable modules #11167

Open

Description

@SimenB

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.

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

Activity

  1. ahnpnl commented on Mar 9, 2021

    @ahnpnl
    Contributor

    I think it’s wise to pin this issue so it stands out to everyone :)

  2. SimenB commented on Mar 9, 2021

    @SimenB
    MemberAuthor

    sure 🙂

  3. pinned this issue on Mar 9, 2021
  4. added this to the Jest 27 milestone on Apr 22, 2021
  5. theoludwig commented on Apr 22, 2021

    @theoludwig
    Contributor

    I think we can already drop the support node 10 for jest@27, there is no need to wait for jest@28 as it will reach EOL. 👍

  6. ahnpnl commented on Apr 30, 2021

    @ahnpnl
    Contributor

    D-Day for Node 10 is here xD

  7. SimenB commented on Apr 30, 2021

    @SimenB
    MemberAuthor

    Jest 27 will support Node 10 so people who are stuck on older versions (of which there are many) can use it

  8. gilles-yvetot commented on May 3, 2021

    @gilles-yvetot

    @SimenB you listed globalSetup as one of thing to migrate to ESM but I don't see setupFiles. Is it an omission or it will be taken care with globalSetup?

  9. SimenB commented on May 4, 2021

    @SimenB
    MemberAuthor

    setupFiles are part of the test run, so that already works 🙂

  10. gilles-yvetot commented on May 4, 2021

    @gilles-yvetot

    Maybe that's one of the modules I am using but I cannot use ESM in my setupFiles with the current version 27.0.0-next.9

  11. SimenB commented on May 4, 2021

    @SimenB
    MemberAuthor

    Could you open up a new issue with a reproduction?

  12. SimenB commented on May 18, 2021

    @SimenB
    MemberAuthor

    I want to do reporters and resolver, then release v27. Anyone up for sending PRs for those?

  13. akauppi commented on May 19, 2021

    @akauppi

    Maybe that's one of the modules I am using but I cannot use ESM in my setupFiles with the current version 27.0.0-next.9

    Hi @gilles-yvetot

    If you want to fix the setupFiles thing, you can compare your approach to what I'm doing in firebase-jest-testing. It uses both Jest 27.0.0-next.9 and ESM globalSetup.

  14. 5 remaining items

  15. thernstig commented on May 26, 2021

    @thernstig
    Contributor

    I tested it, and it does work 😆

  16. chentsulin commented on Oct 28, 2021

    @chentsulin
    Contributor

    Hi @SimenB,
    I just try to help doing these parts:

    • dependencyExtractor
    • prettierPath
    • snapshotResolver
    • snapshotSerializers

    After I dived into it, I found there're some problems that I can't solve by myself.

    dependencyExtractor

    dependencyExtractor is used in the constructor:

    https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-haste-map/src/index.ts#L241-L250

    So the only chance I could find is to make the whole HasteMap factory async like this:

    await HasteMap.create(...);

    Is this a right approach?

    prettierPath

    prettierPath is required using a helper called requireOutside:

    https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-snapshot/src/InlineSnapshots.ts#L53-L60

    This requireOutside function will be transformed by babel-plugin-jest-require-outside-vm:

    https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/scripts/babel-plugin-jest-require-outside-vm.js#L13-L21

    I currently don't have enough knowledge about how to handle this with ESModules.

    snapshotResolver & snapshotSerializers

    Those two are relying on the localRequire function:

    https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-snapshot/src/SnapshotResolver.ts#L79-L85

    https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-jasmine2/src/setup_jest_globals.ts#L95-L104

    So, should we pass runtime.unstable_importModule.bind(runtime) as localImport to solve this problem?

    https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts#L34

    https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-runtime/src/index.ts#L622

  17. SimenB commented on Oct 28, 2021

    @SimenB
    MemberAuthor

    Wonderful, thanks!

    dependencyExtractor

    Yeah, that seems correct. Makes it a breaking change though, so need to wait for Jest 28.

    prettierPath

    Good point, we might have to leave that alone. This is for a specific module (prettier) though, so not as likely that people plug in their own thing which is ESM. If (when) prettier migrates to ESM only, we must change, but up until that leaving it as require seems sensible.

    snapshotResolver & snapshotSerializers

    Yeah, passing in unstable_importModule makes sense. Should probably also pass in https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-runtime/src/index.ts#L421

  18. chentsulin commented on Oct 28, 2021

    @chentsulin
    Contributor

    Passing localRequire, unstable_importModule and unstable_shouldLoadAsEsm through multiple layers looks a little bit tricky and breaks function signature unavoidably.

    How about replacing localRequire with a localRequireOrImportModule function?

  19. SimenB commented on Oct 28, 2021

    @SimenB
    MemberAuthor

    That should work as well 👍

  20. F3n67u commented on Apr 24, 2022

    @F3n67u
    Contributor

    @SimenB is there any work left to do? I am very glad to push these things forward.

  21. SimenB commented on Apr 24, 2022

    @SimenB
    MemberAuthor

    Great! The ones not crossed out in the OP are still missing. Not sure how feasible they are

  22. deleted a comment from 337Gslime on Apr 24, 2022
  23. damianobarbati commented on Oct 10, 2022

    @damianobarbati

    @SimenB is this waiting for nodejs team to provide support?

  24. RanylFoumbi commented on Apr 25, 2023

    @RanylFoumbi

    HI @SimenB
    i'm currently facing badly issue using jest ES6 to mock axios in nodejs
    jest doesn't mock axios like when using ESM like it was when using require to import module
    Please take a look here axios/axios#5676 (comment)

  25. carlocorradini commented on Aug 16, 2023

    @carlocorradini

    Is it possible to make expect ESM compatible?
    ESM compatibility is worthwhile since it can be installed as a separate module/dependency.

  26. Anutrix commented on Sep 21, 2023

    @Anutrix

    Good point, we might have to leave that alone. This is for a specific module (prettier) though, so not as likely that people plug in their own thing which is ESM. If (when) prettier migrates to ESM only, we must change, but up until that leaving it as require seems sensible.

    Prettier seems to have moved on to ESM: https://prettier.io/blog/2023/07/05/3.0.0.html.

  27. usrrname commented on Sep 25, 2023

    @usrrname

    🎻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions