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.

[Bug]: Using `esm` package results in an empty object in Jest >= 25

See original GitHub issue

Version

27.5.1

Steps to reproduce

  1. Clone repo from https://github.com/rschristian/jest-esm-repro
  2. yarn
  3. node index.js -> output is { default: [ { foo: 'bar' } ] }
  4. yarn jest ./index.test.js -> test fails, output is { default: {} }
  5. Downgrade jest to 24.9.0 in package.json
  6. yarn jest ./index.test.js -> test passes, output is { default: [ { foo: 'bar' } ] }

Expected behavior

The test should pass in newer Jest versions.

Actual behavior

The default export is always an empty object.

Additional context

Apologies for this being a bit old, but only just noticing it now. This works correctly in 24.9.0, but as of 25.0.0, it fails. Nothing in the change notes for 25 really stands out to me, and I imagine this should still function properly if it does work in Node itself without issue.

Over in preact-cli, we allow users to write their config files in ESM or CJS, resulting in this setup (use of the esm package). It seemingly is the only way to enable this behavior while also not breaking our test suite due to the upstream import() segfault bug (https://github.com/nodejs/node/issues/35889)

Environment

System:
  OS: Linux 5.16
  CPU: (4) x64 AMD Ryzen 7 5800X 8-Core Processor
Binaries:
  Node: 14.19.0 - /usr/bin/node
  Yarn: 1.22.17 - /usr/bin/yarn
  npm: 6.14.15 - /usr/bin/npm

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
SimenBcommented, Mar 3, 2022

Yeah, I’d try to add a failing test to this repo (possibly in examples/ or something) and then bisect and run that. Linking properly sounds hard 😅 That said, yarn link should also work fine, but probably only into a separate minimal project rather than your own larger one. See e.g. https://github.com/facebook/jest/blob/3f3aa80254f2b2f365825c6ac5b5f0a2217631a5/scripts/verifyPnP.js#L71-L74 (note that you should use yarn v2 or v3 for the link, so yarn link --private --all ../jest or something if your project is a sibling)

1reaction
SimenBcommented, Feb 25, 2022

It is a regression even though we never explicitly supported esm. If the change on our side is trivial (or at least minimal, and also makes sense in isolation), I’m happy to land it. If it’s a hacky change we can close as downstream issue 🙂 Depends on the outcome of the bisect, I guess! 😀

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Require returns an empty object when using jest and ...
You're trying to import server from app.js at the very first line of your test code! import server from "../../app.js".
Read more >
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 >
babel ignores - make sure to include the file in jest's ... - You.com
We could change it to not empty or even allow empty array values to result in not finding any test files. Perhaps with...
Read more >
jest-get-type | Yarn - Package Manager
Fixes for global built in objects in jest-environment-node . Create mock objects in the vm context instead of the parent context. .babelrc is...
Read more >
jest - Awesome JS
[@jest/test-sequencer] Make sure sharding does not produce empty groups ( · [jest-circus] Test marked as todo are shown as todo when inside a...
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