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]: .esm.js is ignored in extensionsToTreatAsEsm

See original GitHub issue

Version

27.1.0

Steps to reproduce

  1. Set extensionsToTreatAsEsm to ['.esm.js'].
  2. Import a path that points to a file named something.esm.js.

Expected behavior

The imported file should be treated as esm.

Actual behavior

The imported file will be treated as CommonJS.

Additional context

AFAICT the culprit is this snippet in packages/jest-resolve/src/shouldLoadAsEsm.ts:

  const extension = extname(path);

  if (extension === '.mjs') {
    return true;
  }

  if (extension === '.cjs') {
    return false;
  }

  if (extension !== '.js') {
    return extensionsToTreatAsEsm.includes(extension);
  }

Which assumes only the last part of the file name needs to be checked against the configured extensions.

Environment

System:
    OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz
  Binaries:
    Node: 14.17.1 - ~/.nvm/versions/node/v14.17.1/bin/node
    npm: 6.14.13 - ~/.nvm/versions/node/v14.17.1/bin/npm
  npmPackages:
    jest: ^27.1.0 => 27.1.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
SimenBcommented, Feb 23, 2022

If you use .js, the closest package.json needs to include type: module, this won’t change.

Also, TypeScript’s tslib uses tslib.es6.js for its ESM build. I would say that’s a pretty common library to have…

Of course, jest could also be smart enough to recognize that tslib’s package.json has "module": "tslib.es6.js", indicating that tslib.es6.js is an ESM. But since it doesn’t even recognize the "module" field without a custom resolver (#2702), that’s probably a stretch…

Jest respects import export condition (as of https://github.com/facebook/jest/releases/tag/v27.3.0), which tslib uses, together with a package.json with the required type: module.

https://github.com/microsoft/tslib/blob/c827964226e85118e2fd35b1cc68d4a5ad867f39/package.json#L32 https://github.com/microsoft/tslib/blob/c827964226e85118e2fd35b1cc68d4a5ad867f39/modules/package.json

We have also come across this issue because Swiper v7 uses .esm.js.

Almost the same thing, but e.g. swiper/react (https://www.runpkg.com/?swiper@8.0.6/package.json#91) support is only available in Jest 28 (currently alpha.4), ref #9771. Only . subpath is support in v27.3.

0reactions
igneosaurcommented, Feb 23, 2022

We have also come across this issue because Swiper v7 uses .esm.js.

https://github.com/nolimits4web/swiper/issues/4871

Read more comments on GitHub >

github_iconTop Results From Across the Web

Importing pure ESM module in TS project fails Jest test with ...
You have to tell Jest to transform those ESM bundled libraries with the transformIgnorePatterns option. This options specifies the ignored ...
Read more >
Configuring Jest
If the file specified by path is not found, an error is returned. For example, with the following configuration: JavaScript; TypeScript. /** @ ......
Read more >
Jest Configuration In Package.Json Fails - ADocLib
Developer Ready: Complete and ready to setup JavaScript testing solution. ... 16 days ago by asingh04 [Bug]:.esm.js is ignored in extensionsToTreatAsEsm.
Read more >
ESM Support | ts-jest - GitHub Pages
To use ts-jest with ESM support: ... Include .ts in extensionsToTreatAsEsm Jest config option. ... to process js/ts with `ts-jest`
Read more >
jest failed to parse a file. this happens e.g. when your code or ...
I thought, but anyways I tried npx jest , got an error as the title, the detailed error as the below: FAIL src/App.test.js...
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