我使用Node 19,并有一个使用TypeScript和内置测试器的小型库。基于这个Github发行评论,我正在尝试测试.ts文件。
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);
});
});当运行npm run test时,我会得到错误
找不到‘/home/./存储库/test/**/*Tests.ts’
有人知道怎么回事吗?
发布于 2022-11-08 12:12:54
编辑:
问题
我还没有测试您的环境,但如果我不得不猜测,我相信是您的npm脚本不正确:
tsc --noEmit && node --loader tsx --test test/**/*Tests.ts在POSIX系统/bin/sh上。模式test/**/*Tests.ts不是由node或npm展开的,而是由/bin/sh来扩展的。但是,** (也称为globstar )不受/bin/sh支持,它没有像您预期的那样被扩展。globstar是,但必须启用。
解决方案
我可能错了,但我相信由于不支持globstar,模式test/**/*Tests.ts变成了test/*/*Tests.ts,它只匹配test文件夹子目录中的文件,比如test/abc/xyzTests.ts。如果您只想匹配test文件夹根目录中的测试,可以将模式重写为test/*Tests.ts,这将与tests/xyzTests.ts匹配,但不匹配test文件夹子目录中的文件。
更好的解决方案
最好是以跨平台的方式重写脚本,而不是依赖于脚本中可能不可靠的平台相关特性。这样,您的脚本甚至应该在Windows上工作。
可能有一种更容易利用另一个包的方法,但我想我应该将它移到JavaScript或TypeScript脚本中。
将脚本更改为:
tsc --noEmit && node --loader tsx runTests.ts然后创建一个名为runTests.ts的文件。
我没有时间试验完整的脚本,但我希望您能够使用套餐来获取文件列表,然后使用节点API调用node --loader tsx --test ...。
相关
https://stackoverflow.com/questions/74358752
复制相似问题