Repository navigation
Test runner: recursive or globbed loading of non-JS test files #44023
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Jul 28, 2022 @nodejs/test_runner
Honestly, I would just add
.tssupport explicitly (and probably.tsx/.jsx) given how common it is compared to the alternative with a helpful message in case people didn't pass a loader/require hook.Reacted by Moshe Atlow and John Kaster- addedtest_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
on Jul 28, 2022 probably for starters, as @benjamingr support
/.tsx?$/as well,
eventually allow configuration of custom file filteringReacted by Benjamin GruenbaumI don’t think supporting ts (or jsx) by default makes any sense if TS/JSX content isn’t auto loaded.
Reacted by Colin Ihrig, Antoine du Hamel and Moshe AtlowPerhaps we should implement a solution for #43895 (that will allow passing a custom function to filter files)
I am willing to implement some
--test-configflag if there is consensus on it.
CC @nodejs/test_runnerMaybe a better solution would be to have an option for the loader to have an option to tell which file patten it supports for tests? /cc @nodejs/loaders
Reacted by Jordan HarbandMaybe a better solution would be to have an option for the loader to have an option to tell which file patten it supports for tests?
why will it be better? I think a configuration file will cover more use-cases (real usecases from here), for example:
- within a file, filter which tests should run
- support glob (without relation to ts support)
loaders should process test files regardless of this discussion, there is a seperate issue about that
Running the test runner on a
.tsfile only makes sense if there's a loader able to understand it, so it's convenient if the loader itself can provide this info. If the info comes from somewhere else, it's more likely to de-sync at some point. Anyway that was just a thought.Reacted by Jordan Harband and Colin IhrigReacted by Moshe AtlowI completely agree that that’s the only way to handle it. Nothing should be implicitly included unless node understands it;cams adding a loader should also implicitly include anything the loader handles. Users should only have to configure file extensions if they’re deviating from their loaders’ defaults.
I might not fully understand how loaders work, so correct me in case I am wrong - a loader does not expose what file extensions it supports,
So there is no way for--testto know what to extensions to filter
If we are talking about a new flag/option declaring what extensions a loader supports in the context of running tests - I don't really see a reason that should be part of loaders and not just part of the test runner config?Personally, I see a huge advantage in letting users specify the file pattern precisely: I often find myself putting test fixtures, shared code etc. in the
test/directory (e.g.test/fixtures.ts), along with test files (such astest/foo.test.ts,test/bar.test.ts). In these cases, I would want to use a glob liketest/**/*.test.tsinstead of basing it off of the.tsextension.- addedloadersIssues and PRs related to ES module loaders.Issues and PRs related to ES module loaders.
on Jul 29, 2022 I see mts and cts file extensions were not mentioned above. This is evidence in favor of allowing users and customization hooks to specify.
42 remaining items
- added a commit that references this issue
on Nov 23, 2022 - added a commit that references this issue
on Dec 7, 2022 - added 2 commits that reference this issue
on Jan 3, 2023 - added a commit that references this issue
on May 22, 2026
What is the problem this feature will solve?
As per https://nodejs.org/dist/latest-v18.x/docs/api/test.html#test-runner-execution-model, the test runner can be started as
node --test dirname/to recursively run all test files (ending in.js,.cjsor.mjs) contained in the directorydirname. This unfortunately means that non-JS test files, specifically test files written in TypeScript in my case, will not be found since they end in.tsinstead of.js. The docs say to provide each test file separately, e.g.node --loader=ts-node/esm --test dirname/foo.test.ts dirname/bar.test.tsbut this gets messy very quickly.What is the feature you are proposing to solve the problem?
I see a few options (in order of my preference, starting with most preferred):
node --loader ts-node/esm --test "dirname/**/*.ts"). Note that this does work on Linux, to some extent, since the shell will expand asterisks. This doesn't work on Windows, though, and doesn't work when the string is quoted. Tools like mocha will perform globbing independently of the operating system or shell.node --loader ts-node/esm --test --test-extensions=.ts,.cts,.mts dirname/).node --loader ts-node/esm --test dirname/and ts-node could hook into the directory traversal).What alternatives have you considered?
node --testpointed to an index file that performs the globbing and dynamically imports all actual test files (very ugly; loses process separation between test files)I also opened an issue at ts-node (TypeStrong/ts-node#1853) specifically for TypeScript, to see if they are interested in better test runner support.