Use the Node.js test runner, but with TypeScript via ts-node.
You need to have typescript
installed as a (dev) dependency and must be using Node version 18.7.0 or later.
Imagine it like ts-node --test
, if that command existed.
Install as a dev dependency:
npm i -D ts-node-test
Then add a script to your package.json
:
{
"scripts": {
"test": "ts-node-test test-file1.ts foo/test-file2.ts another-dir/"
}
}
The command syntax is similar to node --test
. Multiple paths can be passed. Directories will be searched recursively
for any files with supported extensions (currently: .js
, .mjs
, .cjs
; .ts
, .mts
, .cts
).
Then, Node's test runner will be started on all files that were found in this process.
You can override the list of extensions by setting an environment variable (TEST_EXTENSIONS
). This list will then be
used instead of the default extensions. For example:
TEST_EXTENSIONS=.test.ts,.test.js ts-node-test test/
The above will recursively look for files in the test/
directory ending in .test.ts
or .test.js
.
The following additional CLI flags are supported, and will be passed along to Node.js when provided:
-
--test-name-pattern <pattern>
* -
--test-reporter <reporter>
* -
--test-reporter-destination <destination>
* --test-only
--watch
--watch-preserve-output
--experimental-test-coverage
Flags marked with *
can be passed multiple times.
TL;DR: Node.js (at the time of writing) does not allow to override the list of extensions that are used when searching for test files. The official recommendation is to list all files explicitly. That is precisely what this CLI wrapper does behind the scenes.
Please refer to the following issues for further information: