You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 22:53:10