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

Can't use node-option with ts-node #5047

Closed
4 tasks done
ericvergnaud opened this issue Dec 13, 2023 · 4 comments
Closed
4 tasks done

Can't use node-option with ts-node #5047

ericvergnaud opened this issue Dec 13, 2023 · 4 comments
Labels
duplicate been there, done that, got the t-shirt...

Comments

@ericvergnaud
Copy link

ericvergnaud commented Dec 13, 2023

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

when running mocha with both ts-node and node-option parameters, ts-node file extensions (.ts) are no longer supported

Steps to Reproduce

  • Create a project with type module, install mocha, typescript, ts-node and ts-loader
  • typescript file test.spec.ts
  • run node node_modules/mocha/bin/mocha.js --no-warnings --loader=ts-node/esm --require ts-node/register --ui bdd --node-option experimental-wasm-gc src/test.spec.ts

Expected behavior:

I expect the test to run

Actual behavior:

When running the above, I get a TypeError: Unknown file extension ".ts"
If I remove --node-option experimental-wasm-gc I don't get the error
If I run node --experimental-wasm-gc node_modules/mocha/bin/mocha.js --no-warnings --loader=ts-node/esm --require ts-node/register --ui bdd src/test.spec.ts, then the test complains that the experimental-wasm-gc option is missing (I'm loading a WebAssembly)

Reproduces how often:

Always

Versions

  • The output of mocha --version:
    10.1.0
  • The output of node_modules/.bin/mocha --version:
    10.1.0
  • The output of node --version:
    v21.3.0
  • Your operating system
    • name and version: MacOS Ventura Version 13.0 (22A380)
    • architecture (32 or 64-bit): 64
  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash
  • Your browser and version (if running browser tests): n/a
  • Any third-party Mocha-related modules (and their versions):
    ts-loader": "^9.4.1
    "ts-node": "^10.9.1",
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version):
    tsc Version 4.9.5

Additional Information

@ericvergnaud
Copy link
Author

It seems the issue could be located in mocha.js lines 84-86, where the presence of node-option drops the already parsed node args (in nodeArgs variable) instead of combining them

@JoshuaKGoldberg
Copy link
Member

Curiosity: why provide both --loader=ts-node/esm and --require=ts-node/register?

The list of steps provided seems reasonable, but I'm not getting the exact same output locally. Could you please post an isolated reproduction that a maintainer can run with only 1-2 commands, such as a GitHub repository, GitHub Gist, or Stackbliz container?

@JoshuaKGoldberg JoshuaKGoldberg added status: waiting for author waiting on response from OP - more information needed and removed unconfirmed-bug labels Jan 21, 2024
@ericvergnaud
Copy link
Author

you need ts-node/register to compile ts on the fly
you need ts-node/esm to use esm module syntax

Here is an example
MochaBug.zip

@JoshuaKGoldberg
Copy link
Member

Got it, thanks! Makes sense. +1 to your (very helpful, thanks!) comment that Mocha is overriding the implicit-ish --loader=ts-node Node option with the explicit --node-option experimental-wasm-gc. If you pass in --node-option loader=ts-node/esm instead, then everything works happily.

$ node node_modules/mocha/bin/mocha.js --no-warnings --require ts-node/register --ui bdd --node-option loader=ts-node/esm --node-option experimental-wasm-gc src/test.spec.ts                     
(node:37672) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)


Hi there!
  ✔ runs a test

  1 passing (1ms)

...where "happily" ignores the loader notice. That's discussed in #5002.

I think we can consider this a docs issue similar to #5002 that it's not clear how to use Node import/loader/require options together. Filed #5087 to track adding docs. It links to mochajs/mocha-examples#76 as a start.

Aside: in general, it's not great practice to upload a .zip. Downloading and extracting arbitrary files from the internet is kind of scary for us maintainers 🫠. An isolated GitHub repository, Gist, Stackblitz, or other more transparent environment would be nice in the future. This zip works for now.

Thanks for filing!

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2024
@JoshuaKGoldberg JoshuaKGoldberg added duplicate been there, done that, got the t-shirt... and removed status: waiting for author waiting on response from OP - more information needed labels Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate been there, done that, got the t-shirt...
Projects
None yet
Development

No branches or pull requests

2 participants