NodeJS recently added a native test runner
I can successfully get it to run typescript tests with ts-node or tsx
node --loader ts-node/esm --test **/*.test.tsnode --loader tsx --test **/*.test.ts
But one major drawback is the glob pattern (**/*.test.ts) is not working as expected. It is only finding *.test.ts files down one directory, and is not recursively finding test files in nested directories. I think the problem is that node isn't treating ** as a recursive glob pattern.
edit: It looks like the testrunner uses this glob library which may not support that syntax...
src/bar.test.tsis foundsrc/foo/bar.test.tsis not found
I'd like to be able to put *.test.ts files anywhere in my app and have the test script execute them. Is there any way to accomplish this?
.
├── package.json
├── src
│ ├── app
│ │ ├── dir
│ │ │ └── more-nested.test.ts
│ │ └── nested-example.test.ts
│ └── example.test.ts
└── tsconfig.json
**is known as globstar and is not available on every system.First possible solution is to use
findfrom the root folder to get a list of files matching a specified pattern (it will look in every subfolder) and then runnode --loader tsx --testfor every found file:find . -name "*.test.ts" -exec node --loader tsx --test {} ';'Another possible solution (reference: https://github.com/nodejs/help/issues/3902#issuecomment-1307124174) is to create a custom script that finds all files and subdirectories using
globpackage and then run tests invokingnode --loader tsx --test ...withchild_process.Check also this: Why can't ** be used (for recursive globbing) in a npm package.json script, if it works in 'npm shell'?