Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using @swc-node/register with Mocha leads to errors #762

Closed
martinslota opened this issue Mar 26, 2024 · 4 comments · Fixed by #766
Closed

Using @swc-node/register with Mocha leads to errors #762

martinslota opened this issue Mar 26, 2024 · 4 comments · Fixed by #766

Comments

@martinslota
Copy link

martinslota commented Mar 26, 2024

When Mocha is used to run test files written in Typescript along with --import=@swc-node/register/esm-register, the following kind of error gets thrown:

Exception during run: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /path/to/test.spec.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async nextLoad (node:internal/modules/esm/hooks:865:22)
    at async nextLoad (node:internal/modules/esm/hooks:865:22)
    at async Hooks.load (node:internal/modules/esm/hooks:448:20)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'

A specific reproduce along with a hacky fix can be found in this repository.

I'm not entirely sure what a proper fix would look like but I'd be happy to help with implementing it.

@yeliex
Copy link
Contributor

yeliex commented Mar 31, 2024

LGTM. I would test later with more cases

Brooooooklyn pushed a commit that referenced this issue Apr 9, 2024
…762 (#766)

* fix(register): fix esm entry resolver for third-party executor, close #762

* Create great-goats-trade.md

* fix: bug fix
yeliex added a commit to yeliex/swc-node that referenced this issue Apr 10, 2024
…wc-project#762 (swc-project#766)

* fix(register): fix esm entry resolver for third-party executor, close swc-project#762

* Create great-goats-trade.md

* fix: bug fix
@martinslota
Copy link
Author

I tried adopting the fix released in version 1.9.1 but it seems that something else is now amiss:

$ npm test

> swc-node-register-mocha-bug@2.0.0 test
> node --import @swc-node/register/esm-register node_modules/mocha/bin/mocha test.spec.ts

file:///path/to/swc-node-register-mocha-bug/node_modules/mocha/bin/mocha.js:9
 */ const { loadOptions } = require('../lib/cli/options');
                            ^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///path/to/swc-node-register-mocha-bug/node_modules/mocha/bin/mocha.js:9:29
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.11.0

@yeliex Could we maybe reopen this issue? Or is it better if I report a separate new one?

@martinslota
Copy link
Author

I opened a separate issue for the problem described above.

@yeliex
Copy link
Contributor

yeliex commented Jun 3, 2024

@martinslota see #781

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants