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

执行jest命令提示无测试用例,却检测到1个匹配的测试文件

Jest检测到测试用例却不执行的问题
$ jest
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/igormakowski/Documents/repositories/nestjs-template
  9 files checked.
  testMatch:  - 0 matches
  testPathIgnorePatterns: /Users/igormakowski/Documents/repositories/nestjs-template/node_modules/, /Users/igormakowski/Documents/repositories/nestjs-template/dist/, /Users/igormakowski/Documents/repositories/nestjs-template/.stryker-tmp/, .js - 0 matches
  testRegex: .*\.spec\.ts$ - 1 match
Pattern:  - 0 matches
error Command failed with exit code 1.

如上所示,Jest明明检测到1个匹配testRegex规则的测试用例,但却提示未找到测试并以错误码退出。我有另一个配置逻辑相似的项目,运行Jest一切正常。

Jest配置文件 jest.config.ts

import type { Config } from 'jest'

const configuration: Config = {
  moduleFileExtensions: ['js', 'json', 'ts'],
  rootDir: '.',
  testRegex: '.*\\.spec\\.ts$',
  // testMatch: ['<rootDir>/src/**/*.spec.ts'],
  transform: {
    '^.+\\.(t|j)s$': 'ts-jest',
  },
  verbose: true,
  collectCoverageFrom: ['**/*.(t|j)s'],
  testPathIgnorePatterns: [
    '<rootDir>/node_modules/',
    '<rootDir>/dist/',
    '<rootDir>/.stryker-tmp/',
    '.js',
  ],
  moduleNameMapper: {
    '^~/(.*)$': '<rootDir>/src/$1',
    '^@modules/app(.*)$': '<rootDir>/src/modules/$1',
    '^@common/filters(.*)$': '<rootDir>/src/common/$1',
  },
  coveragePathIgnorePatterns: [
    '.*\\.(interface|module|schema|entity|dto|enum|d).ts',
    '.*\\.e2e-spec.ts',
    'index.ts',
    'main.ts',
  ],
  coverageDirectory: './coverage',
  testEnvironment: 'node',
  roots: ['<rootDir>/src'],
}

export default configuration

环境信息

System:
    OS: macOS 13.2.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 18.14.2 - ~/.nvm/versions/node/v18.14.2/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.14.2/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.14.2/bin/npm
  npmPackages:
    jest: 29.5.0 => 29.5.0

已尝试的解决手段

  • 升级Jest到29.6.1版本
  • 用testMatch替换testRegex(配置中已注释相关代码)
  • 重新安装所有依赖
  • 重载VSCode项目
  • 执行以下Jest命令:
    jest --testMatch **/src/**/*.spec.ts
    
    jest --config jest.config.ts
    
    ./node_modules/.bin/jest --config jest.config.ts
    

所有尝试后问题仍未解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:47:38