Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

javascript - How can I ignore a file pattern for Jest code coverage?

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

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

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

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

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

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"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

{
    "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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...