【问题标题】:How to skip a file from executing in jest?如何在玩笑中跳过文件执行?
【发布时间】:2021-01-13 17:09:17
【问题描述】:

我正在使用 jest 进行集成测试,其中一个文件夹下有 20 个测试文件。我需要确保在运行测试时不需要执行该文件夹中的三个文件。我尝试使用 testPathIgnorePatterns 但它仅适用于文件夹而不适用于文件。该怎么做?

jest.config.js

/**
 * @file Jest Integration Test Configuration File
 */

module.exports = {
  roots: ['../../tests'],
  testRegex: '_*_integration.test.(js|ts|tsx)?$',
  globals: {
    'ts-jest': {
      tsconfig: 'tsconfig.json',
    },
  },
  moduleFileExtensions: ['ts', 'js'],
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  testPathIgnorePatterns: ['tests/api/modules/m3/sample.test.ts'],
  testEnvironment: 'node',
  reporters: [
    'default',
    [
      '../../node_modules/jest-html-reporter',
      {
        pageTitle: 'Integration Test Report',
        outputPath: 'tests/reports/integration-test-report.html',
        includeFailureMsg: true,
      },
    ],
  ],
};

【问题讨论】:

  • 为什么不更改文件名,或者将其移动到 lm 例如示例目录并忽略它?
  • 不,这是不可能的。遵循项目的一些标准。所以不建议移动到文件夹@jonrsharpe
  • 什么标准?并且不推荐!== 不可能,拥有一个看似是测试文件但不是的文件真的明智吗(它是什么)?这显然会混淆测试发现,但更重要的是可能会混淆 人们
  • @muthu 如何将文件重命名为_integration.test.skip.js
  • 这是一个答案?你告诉我你不能重命名文件。

标签: javascript node.js jestjs jasmine


【解决方案1】:

使用--testPathIgnorePatterns cli 选项怎么样?

yarn test --testPathIgnorePatterns=user.test.ts

如果是多文件,可以在下面使用。

yarn test --testPathIgnorePatterns=filename1 filename2

【讨论】:

  • 他们已经在使用 testPathIgnorePatterns,并报告它匹配目录而不是文件;为什么 CLI 与配置文件会有所不同?
【解决方案2】:

你可以像这样使用Zest json的collectCoverageFrom参数,

collectCoverageFrom: ['path-to-file1','path-to-file2'],

注意:此选项需要将 collectCoverage 设置为 true 或使用 --coverage. 调用 Jest

参考documentation

在文档中,他们指定了一种模式,但它也适用于文件静态文件路径。

【讨论】:

    猜你喜欢
    • 2020-12-02
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多