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]: problems when testing an ES module

See original GitHub issue

Version

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:open
  • Created 9 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mrazauskascommented, Dec 11, 2022

In ESM you should import from './index.js'.

1reaction
mrazauskascommented, Dec 11, 2022

Thanks. The updated repo does not reproduce the issue anymore. Running rpm run test now I get a setup related error:

Screenshot 2022-12-11 at 17 12 55
Read more comments on GitHub >

github_iconTop 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 >

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