我正在使用 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,
},
],
],
};
使用
--testPathIgnorePatterns
cli 选项怎么样?
yarn test --testPathIgnorePatterns=user.test.ts
如果是多文件,可以使用下面的方法。
yarn test --testPathIgnorePatterns=filename1 filename2
对于 Jest 覆盖范围中要忽略的单个文件,请将以下行放在顶部
/* istanbul ignore file */
忽略个别功能请跳过该功能上方的内容
/* istanbul ignore next */
const ignoreFunc =()=>{
console.log('This function will be ignored from coverage')
}
您可以像这样使用 Zest json 的
collectCoverageFrom
参数,
collectCoverageFrom: ['path-to-file1','path-to-file2'],
注意:此选项需要将collectCoverage设置为true或使用
--coverage.
调用Jest
参考文档
在文档中,他们指定了一种模式,但它也适用于文件静态文件路径。