question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ESM support for jest

See original GitHub issue

Description

Angular package format v13 seems like it won’t be shipping libraries as commonjs anymore and will only include esm bundles. Right now if you try and use an esm node_module inside of a jest unit test it doesn’t work. If there is already discussion about this elsewhere, or there is already a plan for how to deal with this, feel free to close this issue 😄

Motivation

It won’t be possible to run unit tests with angular 13 otherwise.

Suggested Implementation

Use the esm support provided by jest and jest-preset-angular.

Alternate Implementations

Use a custom jest transformer to downlevel the esm in node_modules into commonjs. Having to always run nx test with NODE_OPTIONS=--experimental-vm-modules isn’t an ideal solution. Or maybe it’s possible to persuade the angular team to continue shipping a commonjs bundle to npm until the esm story inside of jest is no longer experimental.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:13
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
mattlewis92commented, Oct 1, 2021

I messed about with transpiling node_modules esm -> commonjs today and I think this may work as a decent solution and can easily be auto migrated via nx:

  1. npm i -D jest-esm-transformer
  2. For each libraries jest.config.js (maybe angular libs only? But more and more packages are starting to ship ESM only so maybe it makes sense for all libs) set these options:
  transform: {
    'node_modules\\/.+\\.js$': 'jest-esm-transformer',
    '^.+\\.(ts|js|html)$': 'jest-preset-angular', // probably tweak this regexp to exclude anything in node_modules
  },
  transformIgnorePatterns: [] // by default jest ignores anything in node_modules: https://jestjs.io/docs/configuration#transformignorepatterns-arraystring
  1. update the nx resolver to support the module field according to the guide here (it looks like APF v13 will only provide the module and exports fields, neither of which are supported out the box by jest)

The only downside of this approach is that there will be a slight perf hit to transpiling node_modules, but I think jest’s built in caching should mitigate most of that, my initial tests on our app there didn’t seem to be much of an observable perf change with this change.

1reaction
barbados-clemenscommented, Aug 12, 2022

@FacePalmDev that change is already in the nx resolver today

@Nightbr you more than likely just need to set up your transformIgnorePatterns to handle the react-dnd package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ECMAScript Modules - Jest
Jest ships with experimental support for ECMAScript Modules (ESM). The implementation may have bugs and lack features. For the latest status ...
Read more >
ESM Support | ts-jest - GitHub Pages
ESM Support · Check ESM Jest documentation. · Enable useESM true for ts-jest config. · Include .ts in extensionsToTreatAsEsm Jest config option.
Read more >
How to use ESM tests with jest? - Stack Overflow
Here are the steps I took to run Jest with a test using ESM. The source files under test were also written using...
Read more >
Setting up Jest with ESM - gists · GitHub
Here are some different ways on how to set up Jest to support ESM. This applies for Jest v25, Node v13, and Babel...
Read more >
Jest + ESM: Almost There... - SitePen
Jest is one of the most popular testing frameworks, but it hasn't really kept up with the growing support for native JavaScript modules ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found