Coverage quality guard, will fail your tests if coverage in changed and added files is below a defined threshold
Checks all locally committed and uncommitted files on the current branch
-
Install jest-coverage-guard in your project
Install with npm:
npm install --save-dev jest-coverage-guard
Install with yarn:
yarn add jest-coverage-guard --dev
-
Create config file (
coverage.guard.config.json) in your project root:(optional: "excludeKeywords", "excludeFiles", "addedFilesQualityGate", "changedFilesQualityGate")
{ "appRootRelativeToGitRepo": "src", "excludeKeywords": [ "// skip-coverage-check", "backbone", "class SomeUglyClass" ], "featureNameRegExp": { "body": "APP-[0-9]+", "flags": "g" }, "excludeFiles": [ "-test.js", ".less"], "addedFilesQualityGate": { "statements": 100, "branches": 100, "functions": 100, "lines": 100 }, "changedFilesQualityGate": { "statements": 80, "branches": 80, "functions": 80, "lines": 80 } } -
In your jest config file
jest.config.jsonadd jest-coverage-guard as a reporter:"reporters": ["default", "jest-coverage-guard"], -
Run jest tests
appRootRelativeToGitRepo- path to your application source code !important: this path should be relative to git repository root and not to CWDexcludeKeywords- keywords that will be used to skip a file from coverage. If a file contains one of the keywords the coverage check script will ignore this file.featureNameRegExp- patters to search commits by commit message. Assuming you are adding in each commit message a issue/ticket number the the beginning. e.g: 'APP-12345 fixed a nasty bug'excludeFiles- array of file extensions that should be ignored from checkaddedFilesQualityGate- config object where you specify quality gate for newly added files, defaults to:
{
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100
}changedFilesQualityGate- config object where you specify quality gate for changed files, defaults to:
{
"statements": 80,
"branches": 80,
"functions": 80,
"lines": 80
}You can use this to fail your CI pipeline when code coverage is not good enough.
This script should be executed only after jest generated the coverage report, so as jest custom reporter.
This script with check your committed and uncommitted files.
Locally the script will check coverage on all files that you have changed and committed implementing current feature and all files that you are currently working with but not yet committed.
In CI environment it will only check committed files for this feature
If you want to exclude your file from coverage_check for some reason (it should be in excludeKeywords config):
add a comment to your file with "skip-coverage-check", eg. // skip-coverage-check
-
The script assumes that you are working with a project management software where you have ticket/issue numbers that are appended or included in every commit message, eg: "APP-12345 fixed typo in awesomefile.js"
-
Script gets the name of the current branch you are working on (config: featureNameRegExp)
-
Extracts the ticket number from the branch name eg. APP-12345 (cconfig: featureNameRegExp)
-
Gets all commits that contain this ticket number in the message.
-
Gets all url's of the files you have changed in those commits.
-
Filters the file URL's to get only app files (config: appRootRelativeToGitRepo) and skips the files that contain exclude keywords (config: excludeKeywords).
-
Finds report for each file from jest coverage report object.
-
Compares the results with corresponding quality gate mask.
-
Adds results to results table.
-
Adds failed coverage errors to the errors table.
-
Shows the result table.
-
Shows errors table.
-
If error table contains errors - fail the script with exit
code 1. 14. If error table contains error but you are locally in--watchor--watchAllmode - script will not fail. -
If error table contains no errors - script will succeed.
NEXT STEPS ONLY LOCALLY:
-
Gets current git.status Object.
-
Gets all files that you have changed but not yet committed.
-
Gets file URL's and then does everything from step 6 to 15.