-
-
Notifications
You must be signed in to change notification settings - Fork 544
Description
Desired Behavior
A native test runner was added in Node 18: https://nodejs.org/dist/latest-v18.x/docs/api/test.html
It can be started by node --test testfile1.js testfile2.js
or node --test testdirectory/
. Trying to use it with TypeScript is a bit cumbersome, though, since the directory variant doesn't quite work:
node --loader ts-node/esm --test testdirectory/
The above will not find any tests written in TypeScript, since only .js
, .cjs
, .mjs
extensions are considered.
See https://nodejs.org/dist/latest-v18.x/docs/api/test.html#test-runner-execution-model.
Since ts-node otherwise seems to want to mirror node's CLI behavior, my suggestion is to add a --test
flag to ts-node that behaves like node would, but including resolution of TS test files. This could be implemented by searching the paths given as positional args to find all files matching .js
, .cjs
, .mjs
, .ts
, .cts
, .mts
, then spawning a node child process like node --loader=ts-node/esm --test FILES
where FILES
is the explicit list of files found in the previous step.
Is this request related to a problem?
I would really like to get rid of third-party testing frameworks, especially for the myriad of small packages that don't require any advanced functionality, but this is hindered by not being able to run TypeScript tests easily.
Alternatives you've considered
This works fine:
node --loader ts-node/esm --test foo.test.ts bar.test.ts
But we really don't want to specify each test file in the command. Globbing is not supported (at least not on Windows):
node --loader ts-node/esm --test test/*.test.ts
outputs:
Could not find 'C:\Users\meyfa\project\test\*.ts'
One might also start node --test
pointing at an index file, that performs the globbing and dynamically imports all actual test files.
I haven't tried this, and it would be very ugly, IMHO. Additionally, it would lose process separation between test files. (node --test foo.js bar.js
will execute foo.js
and bar.js
in separate processes).
Additional context
I understand that the Node test runner is still experimental, so perhaps adding any kind of support for it to ts-node may seem premature.
Additionally, I have opened an issue at Node.js with a request to add globbing and/or a CLI flag for additional test file extensions, see nodejs/node#44023.
Still, I feel like having support for ts-node --test
isn't too far fetched. Especially when Node adds globbing/extension support, the implementation will come almost for free.