[Bug]: .esm.js is ignored in extensionsToTreatAsEsm
See original GitHub issueVersion
27.1.0
Steps to reproduce
- Set
extensionsToTreatAsEsm
to['.esm.js']
. - 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:
- Created 2 years ago
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you use
.js
, the closestpackage.json
needs to includetype: module
, this won’t change.Jest respects
import
export condition (as of https://github.com/facebook/jest/releases/tag/v27.3.0), whichtslib
uses, together with apackage.json
with the requiredtype: module
.https://github.com/microsoft/tslib/blob/c827964226e85118e2fd35b1cc68d4a5ad867f39/package.json#L32 https://github.com/microsoft/tslib/blob/c827964226e85118e2fd35b1cc68d4a5ad867f39/modules/package.json
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 (currentlyalpha.4
), ref #9771. Only.
subpath is support in v27.3.We have also come across this issue because Swiper v7 uses
.esm.js
.https://github.com/nolimits4web/swiper/issues/4871