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

Nuxt/Vue2项目Jest识别测试套件但静默不执行测试如何排查

Nuxt/Vue v2 项目 Jest 单元测试执行异常问题

问题表现

此前项目单元测试可正常运行,能够正确返回测试通过/不通过结果;完成部分代码变更后,Jest 不再执行所有已编写的测试用例,运行时既不返回通过结果也不返回失败结果,运行命令后的输出如下:

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 0 of 10 total
Tests:       0 total
Snapshots:   0 total
Time:        3.454 s, estimated 10 s
Ran all test suites.

已尝试的排查操作均未解决问题:

  • 移除不需要的代码、暂存变更后重新运行测试,仍无任何测试套件被执行
  • 切换到之前测试可正常运行的历史代码版本,问题依然存在
  • 创建空的测试套件,测试运行器也不会抛出your test suite must contain at least one test的异常
  • 问题同时影响tests/unit目录及其他目录下的所有测试文件
  • 曾删除node_modules目录后重新全量安装依赖,问题未修复

问题预判

日常代码提交流程较为规范,但不排除操作过程中引入了配置类错误:

  • 依赖树状态异常,可能存在核心依赖未正确写入package.json的情况
  • 存在此前未触发过的依赖版本不匹配问题

提问者此前一直使用Mocha/Chai开展测试工作,对Jest的使用尚不熟悉,需要明确该问题的可能诱发原因,以及这类问题的标准排查方法。

项目现有配置

jest.config.js 配置内容

module.exports = {
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/$1",
    "^~/(.*)$": "<rootDir>/$1",
    "^vue$": "vue/dist/vue.common.js",
  },
  moduleFileExtensions: ["ts", "js", "vue", "json"],
  transform: {
    "^.+\.ts$": "ts-jest",
    "^.+\.js$": "babel-jest",
    ".*\.(vue)$": "vue-jest",
  },
  collectCoverage: true,
  collectCoverageFrom: [
    "<rootDir>/some-dir/**/*.(js|ts)",
  ],
  testEnvironment: "jsdom",
  testMatch: ["**/*.spec.(js|jsx|ts|tsx)", "**/*.test.(js|jsx|ts|tsx)"],
  runner: "groups",
};

package.json 相关配置内容

{
  "scripts": {
    "test:unit": "jest"
  },
  "dependencies": {
    "core-js": "^2.6.12",
    "nuxt": "^2.15.8",
    "path-to-regexp": "^6.2.1",
    "vue": "^2.6.14",
    "vue-server-renderer": "^2.6.14",
    "vue-template-compiler": "^2.6.14",
    "webpack": "^4.46.0"
  },
  "devDependencies": {
    "@nuxt/types": "^2.15.8",
    "@nuxt/typescript-build": "^2.1.0",
    "@nuxtjs/device": "^2.1.0",
    "@vue/test-utils": "^1.3.0",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "^27.4.4",
    "jest": "^27.4.4",
    "jest-runner-groups": "^2.2.0",
    "ts-jest": "^27.1.1",
    "vue-jest": "^3.0.4"
  }
}

内容的提问来源于stack exchange,提问作者Pawel Kam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 21:42:20