tsx
tsx copied to clipboard
Support new experimental built-in node test runner
Feature request
NodeJS has a new experimental built-in test runner in version 18+. It is typically invoked for JavaScript files via node --test or node --test test/**/*.test.js, for example.
I would like to be able to invoke the new node test runner via tsx:
npx tsx --test- ideal, but the test runner doesn't pick up.tsfiles by defaultnpx tsx --test test/**/*.test.ts- explicit path(s) to test files
Currently when I try those commands, it does start the test runner and prints out the first line, then hangs and has to be interrupted. Looks like it is starting the node REPL, so maybe tweaking that behavior is all that's needed.
$ npx tsx --test test/**/*.test.ts
TAP version 13
# !!! Hangs here until I press Ctrl+C
# Subtest: /home/neodon/.local/share/pnpm/store/v3/tmp/dlx-34798/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/repl.js
not ok 1 - /home/neodon/.local/share/pnpm/store/v3/tmp/dlx-34798/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/repl.js
---
duration_ms: 6.935629576
failureType: 'cancelledByParent'
error: 'test did not finish before its parent and was cancelled'
code: 'ERR_TEST_FAILURE'
stack: |-
process.emit (node:events:513:28)
process.emit (/home/neodon/.local/share/pnpm/store/v3/tmp/dlx-34798/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/suppress-warnings.cjs:1:307)
...
# Subtest: /home/neodon/p/labs/test/foo.test.ts
ok 2 - /home/neodon/p/labs/test/foo.test.ts
---
duration_ms: 0.082254723
...
1..2
# tests 2
# pass 1
# fail 0
# cancelled 1
# skipped 0
# todo 0
# duration_ms 6.964441232
Why?
Currently I am able to run the node test runner with tsx by using it as a loader, so this feature is more of a minor thing / polish. It could be more intuitive to new users.
Example:
// test/foo.test.ts
import test from 'node:test'
import assert from 'node:assert/strict'
test('node test!', async t => {
assert.equal(true, true)
})
In package.json, I have "type": "module" set.
I can run the tests with:
$ node --loader tsx --test test/**/*.test.ts
TAP version 13
# Subtest: /home/james/p/labs/test/foo.test.ts
ok 1 - /home/james/p/labs/test/foo.test.ts
---
duration_ms: 0.076968906
...
1..1
# tests 1
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 0.103634573
This is fine for me, but wouldn't it be awesome if you could run tests with tsx --test, analagous to node --test? 😎
Alternatives
No response
Additional context
No response