Gathering detailed insights and metrics for @jobber/jest-a-coverage-slip-detector
Gathering detailed insights and metrics for @jobber/jest-a-coverage-slip-detector
Gathering detailed insights and metrics for @jobber/jest-a-coverage-slip-detector
Gathering detailed insights and metrics for @jobber/jest-a-coverage-slip-detector
Improve visibility and encourage progress towards your test coverage goals
npm install @jobber/jest-a-coverage-slip-detector
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
7 Stars
34 Commits
2 Forks
42 Watching
2 Branches
245 Contributors
Updated on 27 Feb 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-26.7%
1,530
Compared to previous day
Last week
7.6%
9,449
Compared to previous week
Last month
-0.4%
46,405
Compared to previous month
Last year
555.9%
236,965
Compared to previous year
1
4
@jobber/jest-a-coverage-slip-detector
This library ensures that new files have Jest coverage meeting the configured goals.
Additionally, this library can be added to an existing project such that legacy files not meeting the coverage goals are added to an exception list where they raise an error if coverage slips, and ratchet upwards as progress is made improving them, all while enforcing the higher coverage goals on net new code.
collectCoverageFrom
npm install --save-dev @jobber/jest-a-coverage-slip-detector
Within jest.config.js
or jest.config.ts
:
json
in coverageReporters
.--coverage
parameter).coverageThreshold
configuration from Jest, or set it to: coverageThreshold: { global: {} }
.withJestSlipDetection
utility method in order to dynamically leverage collectCoverageFrom
set to the configured coverageGlob
.Example (JavaScript):
1const { withJestSlipDetection } = require("@jobber/jest-a-coverage-slip-detector");
2
3module.exports = withJestSlipDetection({
4 coverageReporters: [
5 "json" // plus any other reporters, e.g. "lcov", "text", "text-summary"
6 ],
7 coverageThreshold: { global: {} },
8});
Example (TypeScript):
1import type { Config } from "@jest/types"; 2import { withJestSlipDetection } from "@jobber/jest-a-coverage-slip-detector"; 3 4const config: Config.InitialOptions = { 5 coverageReporters: [ 6 "json" // plus any other reporters, e.g. "lcov", "text", "text-summary" 7 ], 8 transform: { 9 "^.+\\.ts?$": "ts-jest", 10 } 11}; 12 13export default withJestSlipDetection(config);
These scripts assume you have the following two reporters installed:
npm i -D jest-progress-bar-reporter jest-junit
Within package.json
:
1{ 2 "scripts": { 3 "test": "jest", 4 "test:ci": "jest --runInBand --coverage --reporters=jest-progress-bar-reporter --reporters=jest-junit --ci", 5 "posttest:ci": "npm run test:validateCoverage", 6 "test:generateCoverage": "jest --coverage --reporters=jest-progress-bar-reporter --ci", 7 "test:validateCoverage": "jest-a-coverage-slip-detector", 8 "test:updateCoverageExceptions": "jest-a-coverage-slip-detector --update", // Used to 'ratchet' up coverage after improving it. 9 "test:setCoverageExceptionsBaseline": "jest-a-coverage-slip-detector --force-update" // Sets the baseline for test coverage (accepts any under-target coverage). 10 } 11}
If you're happy with the defaults below, nothing further is needed:
1{ 2 "coverageGoal": { "lines": 80, "functions": 80, "statements": 80, "branches": 80 }, 3 "coverageGlob": [ 4 "**/*.{ts,tsx,js,jsx}", 5 "!**/node_modules/**", 6 "!**/vendor/**", 7 ] 8}
Otherwise:
.jest-a-coverage-slip-detector
directory in the root of your projectconfig.json
file within the .jest-a-coverage-slip-detector
directoryExample:
1{ 2 "coverageGoal": { "lines": 90, "functions": 90, "statements": 90, "branches": 90 }, 3 "coverageGlob": ["./app/javascript/**/*.{ts,tsx,js,jsx}"] 4}
npm run test:generateCoverage && npm run test:validateCoverage
npm run test:setCoverageExceptionsBaseline
generatedCoverageExceptions.json
by default) to source controlnpm run test:ci
in your CI (the key things are that coverage is enabled and that the --ci
argument is present)npm run test:generateCoverage && npm run test:updateCoverageExceptions
and commit the updated exception listing to "ratchet" up the coverage.--report-only
option in the initial rollout, and remove the option once you're ready to require coverage errors to be addressed.If you're leveraging parallelism to do test splitting and running your tests concurrently on CI (e.g. fan-out/fan-in), a few adjustments to the pattern are needed.
posttest:ci
script - you'll need to explicitly invoke coverage validation as a separate step after you gather coverage on the concurrent runs.Use jest
to generate the files to be tested so you ensure you have parity with the test run and coverage gathering used to generate the exceptions:
1TESTFILES=$(npx jest --listTests | sed s:$PWD/:: | circleci tests split --split-by=timings --show-counts)
2npm run test:ci $TESTFILES
json
coverage reports around for a follow-up validation step in your workflow. Ensure these can be located later under the coverage output directory (both jest and mergeCoveragePath
should be set to the same directory). For CircleCI, this means adding them to a workspace folder with unique names:1// example 2COVERAGE_REPORT_SHARD=coverage/coverage-final${CIRCLE_NODE_INDEX}.json 3npm run test:ci $TESTFILES && mv coverage/coverage-final.json $COVERAGE_REPORT_SHARD
test_coverage
) that runs after the concurrent testing is completed.
test:validateCoverage
with the merge
argument: npm run test:validateCoverage -- --merge
.Example config.json
(the mergeCoveragePath
directory should match jest):
1{ 2 ... 3 "mergeCoveragePath": "coverage", 4 ... 5}
1$ jest-a-coverage-slip-detector --help 2Usage: jest-a-coverage-slip-detector [options] 3 4Options: 5 --help, -h Show this help 6 7 --update Update exceptions with improved coverage levels. 8 Used to 'ratchet' up coverage after improving it. 9 10 --force-update Record current coverage errors as exceptions. 11 Used to: 12 - Snapshot current coverage errors as legacy exceptions. 13 - Force accept a reduction in coverage. 14 15 --merge Merges together concurrently collected coverage 16 17 --report-only Exit successfully even if coverage errors are detected.
npm install
in this repo to ensure everything is up-to-datenpm link
to register the package locallynpm link @jobber/jest-a-coverage-slip-detector
jest-a-coverage-slip-detector
- it will run this repo's code directly!After I'm setup with this library, what if I decide to raise the coverage goal higher for new code?
jest-a-coverage-slip-detector/config.json
file and then update snapshots using npm run test:setCoverageExceptionsBaseline
.Do I need to use different test commands on dev than I would on CI?
--runInBand
and --ci
on CI.Why do I only see the coverage errors on CI and not locally?
npm run test:validateCoverage
(e.g. perhaps via a posttest
script).What if I'm running tests locally, will I be slowed down by coverage scanning?
How do I incrementally add test coverage to a previously uncovered file without having testing fail due to the goal being unmet?
collectCoverageFrom
in order to capture snapshots on files even if they are completely untested. This means that as you incrementally add test coverage, you'll be greeted with a message in the CI failure celebrating the improved coverage and asking that you update snapshots to bump up the threshold for that file.What exactly is the purpose of withJestSlipDetection
?
withJestSlipDetection
will intelligently set Jest's internal collectCoverageFrom
. This mechanism also allows some validation of key Jest configuration to be performed, to help identify misconfigurations that would impact this tooling.No vulnerabilities found.
No security vulnerabilities found.