[Bug]: problems when testing an ES module
See original GitHub issueVersion
29
Steps to reproduce
1- clone https://github.com/eng-dibo/issue-gcloud-esm
2- run npm start
-> works
3- run npm test
-> doesn’t work
Expected behavior
the test runs without any problem
Actual behavior
when running the script using ts-node
(via npm start
) it works, but with ts-jest
(via npm t
) this error occurs:
Cannot use import statement outside a module
error stack:
E:\downloads\tmp\node\issue-gcloud-esm\node_modules\abort-controller\dist\abort-controller.mjs:5
import { EventTarget, defineEventAttribute } from 'event-target-shim';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import {
| ^
2 | Bucket,
3 | DownloadResponse,
4 | File,
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1449:14)
at Object.<anonymous> (node_modules/@google-cloud/storage/build/src/resumable-upload.js:17:28)
at Object.<anonymous> (node_modules/@google-cloud/storage/build/src/file.js:30:25)
at Object.<anonymous> (node_modules/@google-cloud/storage/build/src/bucket.js:29:16)
at Object.<anonymous> (node_modules/@google-cloud/storage/build/src/index.js:31:16)
at Object.<anonymous> (index.ts:1:1)
at Object.<anonymous> (index.spec.ts:2:1)
Additional context
No response
Environment
System:
OS: Windows 10 10.0.19044
CPU: (4) x64 Intel(R) Core(TM) i5-4310U CPU @ 2.00GHz
Binaries:
Node: 18.12.0 - C:\Program Files\nodejs\node.EXE
npm: 9.1.2 - C:\Program Files\nodejs\npm.CMD
npmPackages:
jest: ^29.3.1 => 29.3.1
Issue Analytics
- State:
- Created 9 months ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Node.js Bug Or Am I Doing it Wrong? ES Modules [closed]
This is not a bug in nodejs (running ESM modules works just fine). It's a configuration error on your part in some way...
Read more >Unit Testing Challenges with Modular JavaScript Patterns
The first issue is that using ES6's export syntax to share individual functions, but compiling to CommonJS, it's not possible to stub or...
Read more >JavaScript modules - MDN Web Docs
Report problems with this compatibility data on GitHub ... If you don't, you'll get a strict MIME type checking error along the lines...
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 >Using ES modules with CommonJS modules in Node.js
This is not meant to be a comprehensive overview of ES modules and CommonJS modules — I do not go ... "test": "echo...
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
In ESM you should import from
'./index.js'
.Thanks. The updated repo does not reproduce the issue anymore. Running
rpm run test
now I get a setup related error: