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

Support TypeScript extensions when using --test flag. #257

Closed
remcohaszing opened this issue Jun 15, 2023 · 7 comments · Fixed by #410
Closed

Support TypeScript extensions when using --test flag. #257

remcohaszing opened this issue Jun 15, 2023 · 7 comments · Fixed by #410
Labels

Comments

@remcohaszing
Copy link

Feature request

Running node --test discovers test files. However, it doesn’t discover files that end with the .ts, .cts, .mts, or .tsx extensions. It would be nice if tsx supports this.

Why?

I would like to leverage the builtin Node test runner. I also like using tsx. They work great together, except for this quirk meaning I need to specify all tests as command line parameters.

Alternatives

The workaround is to specify test files as command line parameters. This works, but it’s not ideal.

Additional context

https://nodejs.org/api/test.html#test-runner-execution-model

@remcohaszing remcohaszing added enhancement New feature or request pending triage labels Jun 15, 2023
@privatenumber
Copy link
Owner

Happy to accept a PR to add support.

IIRC there isn't an API to extend this. I recommend starting by opening a feature request with Node.js.

@bestickley

This comment was marked as off-topic.

@kasajian

This comment was marked as off-topic.

@privatenumber
Copy link
Owner

privatenumber commented Sep 15, 2023

This issue is for implementing a feature, and these workarounds aren't productive towards that goal so I'm going to hide them.

For anyone encountering this problem:

We're looking for a working approach that actually extends Node.js' --test glob.

Personally, I don't use Node's --test feature so I'm not incentivized to prioritize this. But I'm happy to prioritize and support anyone that wants to take initiative on this.

@privatenumber
Copy link
Owner

privatenumber commented Nov 23, 2023

Investigated this.

Seems like the node --test flag accepts globs from v21, so you can simply pass in one that catches TypeScript files.

Since everything passed into tsx is passed down to Node, you can also pass it into tsx:

tsx --test '**/*.test.ts'

(Note: quoting the glob prevents your shell from expanding it so Node will expand it instead)

The default glob Node uses is here:
https://github.com/nodejs/node/blob/v21.2.0/lib/internal/test_runner/utils.js#L51

So we can alter that to support TS extensions for the default behavior with TS support:

tsx --test '**/{test,test/**/*,test-*,*[.-_]test}.?(c|m)@(t|j)s'

I plan on intercepting the --test flag to use this default instead.

@remcohaszing
Copy link
Author

Dropped support for Node.js 16 also means node:test#run() can be used.

@karlhorky
Copy link

karlhorky commented Nov 24, 2023

Thanks for this issue and #410!

@remcohaszing am I understanding that you are now (as of tsx@4.4.0) able to run all Node.js tests like this?

tsx --test

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants