Skip to content

Commit

Permalink
feat: Implement support for watch mode (#32)
Browse files Browse the repository at this point in the history
Fixes #28. This patch adds a dependency on `yargs` to be able to parse
command-line arguments. If `--watch` is provided, it will be passed
along to the spawned Node.js child process. As a side effect of using
yargs, we now also support the commands `ts-node-test --version` and
`ts-node-test --help`.
  • Loading branch information
meyfa authored Dec 26, 2022
1 parent 334396f commit 8914426
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- windows-latest
node-version:
- 18
- 19
name: test (${{ matrix.os }}) - Node.js ${{ matrix.node-version }}
runs-on: ${{ matrix.os }}
steps:
Expand Down
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ You need to have `typescript` installed as a (dev) dependency and must be using

Imagine it like `ts-node --test`, if that command existed.

### How to use
## Usage

Install as a dev dependency:

Expand All @@ -29,17 +29,28 @@ The command syntax is similar to `node --test`. Multiple paths can be passed. Di
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 also override the list of extensions by setting an environment variable (`TEST_EXTENSIONS`).
Then, the default list of extensions will not be used during discovery and instead only listed extensions will be included.
For example:
### Extensions

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`.

### Why this is needed
### Watch mode

On Node.js versions that support watch mode in conjunction with the test runner, you can use it by passing the `--watch`
argument. At time of writing, this is only possible on Node.js 19, but will likely be backported to Node.js 18 in the
future.

```
ts-node-test --watch test/
```

## Why this is needed

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
Expand Down
138 changes: 131 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
"@meyfa/eslint-config": "2.1.2",
"@types/node": "18.11.17",
"@types/sinon": "10.0.13",
"@types/yargs": "17.0.17",
"eslint": "8.30.0",
"rimraf": "3.0.2",
"sinon": "15.0.1",
"typescript": "4.9.4"
},
"dependencies": {
"ts-node": "10.9.1"
"ts-node": "10.9.1",
"yargs": "17.6.2"
},
"peerDependencies": {
"typescript": "^4.0.0"
Expand Down
18 changes: 17 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env node

import { main } from './index.js'
import yargs from 'yargs'

await main()
const parsedArgs = yargs(process.argv.slice(2)).command('* <paths...>', 'Run tests', (yargs) => {
return yargs.options({
watch: {
type: 'boolean',
default: false,
description: 'Run in watch mode'
}
}).positional('paths', {
type: 'string',
array: true,
demandOption: true,
description: 'Paths to test files'
})
}).parseSync()

await main(parsedArgs.paths, parsedArgs)
Loading

0 comments on commit 8914426

Please sign in to comment.