I'm using Node 19 and have a small library using TypeScript and the builtin testrunner. Based on this Github issue comment I'm trying to test the .ts files.
tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"esModuleInterop": true,
"lib": ["es2022", "dom"],
"module": "commonjs",
"outDir": "build",
"resolveJsonModule": true,
"strict": true,
"target": "es2022"
},
"include": ["./**/*.ts"],
"exclude": ["./build"]
}
package.json
{
"scripts": {
"test": "tsc --noEmit && node --loader tsx --test test/**/*Tests.ts"
},
"dependencies": {
"@types/node": "18.11.9"
},
"devDependencies": {
"tsx": "3.11.0",
"typescript": "4.8.4"
}
}
./test/someTests.ts
import assert from 'assert/strict';
import { describe, it } from 'node:test';
describe('tests', () => {
it('passes', () => {
assert.ok(true);
});
});
When running npm run test I get the error
Could not find '/home/.../repository/test/**/*Tests.ts'
Does someone know what's wrong?
EDIT:
See this example project which I have created.
Problem
I haven't tested your environment, but if I had to guess I believe it is your
npmscript which is incorrect:On a POSIX system
npmwill execute the script with/bin/sh. The patterntest/**/*Tests.tsis not expanded bynodeornpmit is up to/bin/shto expand it. However,**, also known asglobstaris not supported by/bin/sh, it is not being expanded as you would expect. Theglobstaris supported by/bin/bash, but must be enabled.Solution
I could be wrong, but I believe since
globstaris not supported, the patterntest/**/*Tests.tsbecomestest/*/*Tests.tswhich will only match files in a subdirectory of yourtestfolder, such astest/abc/xyzTests.ts. If you just wanted to match tests in the root of thetestfolder, you could rewrite the pattern astest/*Tests.ts, which would matchtests/xyzTests.ts, but would not match files in a subdirectory of yourtestfolder.Better Solution
It would be better to rewrite the script in a cross-platform manner, instead of relying on platform dependent features in your scripts which is may be unreliable. This way your script should even work on Windows.
There might be an easier way to leverage another package, but I think I would just move it into a JavaScript or TypeScript script.
Change your script to:
Then create a file named
runTests.ts.I don't have the time to experiment with a full script, but I expect you would want to use the
globpackage to get the list of files and thechild_processNode API to then callnode --loader tsx --test ....Related
Why can't ** be used (for recursive globbing) in a npm package.json script, if it works in 'npm shell'?
Why you should always quote your globs in NPM scripts.