使用Vitest进行部分测试:如何仅运行指定目录的测试文件?
仅运行指定目录下的测试文件方法
当然可以实现,不同测试工具的具体操作如下:
Jest(Vue2/3项目常用)
- 直接通过命令行指定目录:
# npm npm test -- /test/vue-components/ # yarn yarn test /test/vue-components/ - 也可以在项目的
package.json中配置快捷命令,方便后续重复使用:
配置后直接运行"scripts": { "test:vue-components": "jest /test/vue-components/" }npm run test:vue-components或yarn test:vue-components即可。
Vitest(Vue3主流测试工具)
- 直接指定目标目录运行测试:
npx vitest run test/vue-components/ - 同样可在
package.json中配置快捷命令:"scripts": { "test:vue-components": "vitest run test/vue-components/" }
Cypress(端到端测试场景)
- 使用
--spec参数指定目录下的所有测试文件:npx cypress run --spec "test/vue-components/**/*.spec.js" - 配置快捷命令示例:
"scripts": { "test:e2e:vue": "cypress run --spec 'test/vue-components/**/*.spec.js'" }
核心逻辑是给测试命令传递目标目录(或文件匹配规则)的参数,绝大多数前端测试框架都支持这种方式。
内容的提问来源于stack exchange,提问作者Winns
相关产品推荐
相关产品推荐

