Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" } 
like image 293
Bas van Dijk Avatar asked Jun 22 '18 16:06

Bas van Dijk


People also ask

Does Jest ignore Node_modules?

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

How do I use coveragePathIgnorePatterns?

coveragePathIgnorePatterns [array<string>]​ Use the <rootDir> string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories. Example: ["<rootDir>/build/", "<rootDir>/node_modules/"] .


2 Answers

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.

like image 182
Steven Scott Avatar answered Sep 22 '22 13:09

Steven Scott


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

  collectCoverage: true,   collectCoverageFrom: ['src/**/*.ts','!src/*/filesToExclude.ts'] 
like image 45
Gourab Paul Avatar answered Sep 18 '22 13:09

Gourab Paul