如何忽略 Jest 代码覆盖率的文件模式? [英] How can I ignore a file pattern for Jest code coverage?
问题描述
在我的 Jest 测试项目中,我有 *.entity.ts
文件.我不希望这些文件包含在我的覆盖率测试中.
In my Jest tested project I have *.entity.ts
files. I don't want these files to be included in my coverage test.
根据 https://facebook 上的文档.github.io/jest/docs/en/configuration.html#coveragepathignorepatterns-array-string 有一个 coveragePathIgnorePatterns
设置,您可以在 package.json 中使用它代码>
According to the documentation at https://facebook.github.io/jest/docs/en/configuration.html#coveragepathignorepatterns-array-string there is a coveragePathIgnorePatterns
setting which you can use in the package.json
我尝试过正则表达式和文件模式,但这些都没有忽略最终报告中的 *.entity.ts
文件.
I've tried regex and file patterns but none of these just ignores the *.entity.ts
files in the final report.
当我添加例如 "coveragePathIgnorePatterns": ["common"]
时,我的测试甚至不会再运行了.
When I add for example "coveragePathIgnorePatterns": ["common"]
my tests won't even run anymore.
关于如何让 Jest 在覆盖测试中跳过 *.entity.ts
的任何想法?
Any thoughts on how I can make Jest skip the *.entity.ts
in the coverage tests?
我在 package.json 中的 Jest 部分如下所示:
My Jest section in the package.json looks like:
{
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage"
}
推荐答案
我使用外部 JSON 文件来保存我的 Jest 配置,并使用 npm 从我的 package.json
运行它:jest--config jest.config.json --no-cache
I use an external JSON file to hold my Jest configuration and run it from my package.json
using npm: jest --config jest.config.json --no-cache
jest.config.json
jest.config.json
{
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.ts"
],
"coveragePathIgnorePatterns": [
"node_modules",
"test-config",
"interfaces",
"jestGlobalMocks.ts",
".module.ts",
"<rootDir>/src/app/main.ts",
".mock.ts"
],
"coverageDirectory": "<rootDir>/coverage/",
"coverageThreshold": {
"global": {
"branches": 20,
"functions": 30,
"lines": 50,
"statements": 50
}
},
"mapCoverage": true,
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts",
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!@ionic-native|@ionic|@ngrx|angular2-ui-switch|angularfire2|jest-cli)"
],
"verbose": false
}
我的覆盖范围不包括coveragePathIgnorePatterns"中列出的文件.也许源代码行/src/app/main.ts"是您需要的条目.
My coverage does not include the files listed in the "coveragePathIgnorePatterns". Maybe the source line "/src/app/main.ts" is the entry you need.
这篇关于如何忽略 Jest 代码覆盖率的文件模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!