最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to skip a file from executing in jest? - Stack Overflow

programmeradmin1浏览0评论

I am working on integration tests with jest where i have a 20 test files under a folder. I need to make sure three files in that folder need not to be executed while running the tests. i tried with testPathIgnorePatterns but it only works for folders and not for files. how to do that?

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,
      },
    ],
  ],
};

I am working on integration tests with jest where i have a 20 test files under a folder. I need to make sure three files in that folder need not to be executed while running the tests. i tried with testPathIgnorePatterns but it only works for folders and not for files. how to do that?

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,
      },
    ],
  ],
};

Share Improve this question edited Jan 13, 2021 at 9:09 Sabito 5,1459 gold badges38 silver badges65 bronze badges asked Nov 27, 2020 at 8:38 muthumuthu 8032 gold badges12 silver badges28 bronze badges 6
  • 1 Why not change the name of the file, or move it into a lm e.g. samples directory and ignore that? – jonrsharpe Commented Nov 27, 2020 at 8:39
  • Nope, that is not possible. following some standards over the project. so moving to a folder is not remended @jonrsharpe – muthu Commented Nov 27, 2020 at 8:42
  • What standards? And not remended !== not possible, is it really sensible to have a file that seems to be a test file but isn't (what is it)? It's clearly confusing the test discovery, but more importantly will probably confuse people. – jonrsharpe Commented Nov 27, 2020 at 8:46
  • 1 @muthu how about renaming the files to _integration.test.skip.js – Teneff Commented Nov 27, 2020 at 11:54
  • 2 That's an answer? You told me you couldn't rename the file. – jonrsharpe Commented Nov 27, 2020 at 15:07
 |  Show 1 more ment

2 Answers 2

Reset to default 5

How about using --testPathIgnorePatterns cli option?

yarn test --testPathIgnorePatterns=user.test.ts

If for multi files, can use below.

yarn test --testPathIgnorePatterns=filename1 filename2

You can use the collectCoverageFrom parameter of Zest json like this,

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

Note: This option requires collectCoverage to be set to true or Jest to be invoked with --coverage.

Refer documentation

In documentation, they specified a pattern but it works file static file paths too.

发布评论

评论列表(0)

  1. 暂无评论