javascript Jest 忽略代码覆盖的文件模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50992518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:22:45  来源:igfitidea点击:

Jest ignore file pattern for code coverage

javascriptjestjs

提问by Bas van Dijk

In my Jest tested project I have *.entity.tsfiles. I don't want these files to be included in my coverage test.

在我的 Jest 测试项目中,我有*.entity.ts文件。我不希望这些文件包含在我的覆盖率测试中。

According to the documentation at https://facebook.github.io/jest/docs/en/configuration.html#coveragepathignorepatterns-array-stringthere is a coveragePathIgnorePatternssetting which you can use in the package.json

根据在文档https://facebook.github.io/jest/docs/en/configuration.html#coveragepathignorepatterns-array-string有一个coveragePathIgnorePatterns,你可以在使用设置package.json

I've tried regex and file patterns but none of these just ignores the *.entity.tsfiles in the final report.

我试过正则表达式和文件模式,但这些都没有忽略*.entity.ts最终报告中的文件。

When I add for example "coveragePathIgnorePatterns": ["common"]my tests won't even run anymore.

例如,当我添加时,"coveragePathIgnorePatterns": ["common"]我的测试甚至不再运行。

Any thoughts on how I can make Jest skip the *.entity.tsin the coverage tests?

关于如何让 Jest 跳过*.entity.ts覆盖测试的任何想法?

My Jest section in the package.json looks like:

我在 package.json 中的 Jest 部分如下所示:

{
    "moduleFileExtensions": [
        "js",
        "json",
        "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
        "^.+\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage"
}

回答by Steven Scott

I use an external JSON file to hold my Jest configuration and run it from my package.jsonusing npm: jest --config jest.config.json --no-cache

我使用外部 JSON 文件来保存我的 Jest 配置并从我package.json使用的 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
}

My coverage does not include the files listed in the "coveragePathIgnorePatterns". Maybe the source line "/src/app/main.ts" is the entry you need.

我的覆盖范围不包括“coveragePathIgnorePatterns”中列出的文件。也许源代码行“/src/app/main.ts”是您需要的条目。

回答by Gourab Paul

You can add a ! mark in collectCoverageFrom parameter to exclude it from counting. Here any file inside a subfolder of src dir

您可以添加一个!在 collectCoverageFrom 参数中标记以将其从计数中排除。这里是 src 目录的子文件夹中的任何文件

  collectCoverage: true,
  collectCoverageFrom: ['src/**/*.ts','!src/*/filesToExclude.ts']