首页 > 解决方案 > 尝试在 Jest 中使用 -t 或 --testNamePattern 会运行所有测试

问题描述

我正在按照他们的文档中描述的方式使用jestts-jest配置。ts-jest

如果我运行,yarn test --listTests我可以看到我想要运行的测试文件:processNewUser.ts包含在__test__我项目文件夹的输出中。

我只能用yarn test --testPathPattern='processNewUser'

但是如果我尝试使用yarn test --testNamePattern='Processes new user. Auth'哪个是测试的名称,那么每个测试都会运行,包括所有没有指定名称字符串的测试。

与: yarn test -t="Auth" yarn test -t Auth yarn test --testNamePattern "Auth" jest Auth jest -t="Processes" 等等等等以及每个语法组合都相同。还尝试了命名describe函数包装器而不是测试。没运气。

tsconfig.json的是:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "outDir": "dist",
    "sourceMap": true,
    "allowJs": true,
    "strict": true,
    "noImplicitAny": false,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "removeComments": false,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "lib": [
      "ES2020.Promise",
      "ES2015.Iterable",
      "ES2015.Symbol.WellKnown"
    ],
  },
  "include": ["src/**/*"],
}

jest.config.ts的是:

import type {Config} from '@jest/types';
const config: Config.InitialOptions = {
  clearMocks: true,
  coverageDirectory: 'coverage',
  coverageProvider: 'v8',
  moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
  preset: 'ts-jest',
  setupFiles: ['dotenv/config'],
  setupFilesAfterEnv: ['./jest.setup.js'],
  testEnvironment: 'node',
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
    '^.+\\.ts?$': 'ts-jest',
  },
  transformIgnorePatterns: ['/node_modules/', '\\.pnp\\.[^\\/]+$'],
  
}

export default config;

纱线脚本只是:"test": "jest",

我希望它能够标记测试,auth例如让所有身份验证测试运行。

有什么帮助吗?

标签: typescriptunit-testingtestingjestjsts-jest

解决方案


按名称过滤测试的谓词在此处定义:

  if (globalConfig.testNamePattern) {
    const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
    env.specFilter = (spec: Spec) => testNameRegex.test(spec.getFullName());
  }

请注意,它会根据每个规范的全名检查创建的正则表达式。即测试套件本身的名称加上所有周围描述块的名称。


推荐阅读