
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
@jobber/jest-a-coverage-slip-detector
Advanced tools
Ensures that new files have jest coverage and that legacy files only increase
@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):
const { withJestSlipDetection } = require("@jobber/jest-a-coverage-slip-detector");
module.exports = withJestSlipDetection({
coverageReporters: [
"json" // plus any other reporters, e.g. "lcov", "text", "text-summary"
],
coverageThreshold: { global: {} },
});
Example (TypeScript):
import type { Config } from "@jest/types";
import { withJestSlipDetection } from "@jobber/jest-a-coverage-slip-detector";
const config: Config.InitialOptions = {
coverageReporters: [
"json" // plus any other reporters, e.g. "lcov", "text", "text-summary"
],
transform: {
"^.+\\.ts?$": "ts-jest",
}
};
export 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
:
{
"scripts": {
"test": "jest",
"test:ci": "jest --runInBand --coverage --reporters=jest-progress-bar-reporter --reporters=jest-junit --ci",
"posttest:ci": "npm run test:validateCoverage",
"test:generateCoverage": "jest --coverage --reporters=jest-progress-bar-reporter --ci",
"test:validateCoverage": "jest-a-coverage-slip-detector",
"test:updateCoverageExceptions": "jest-a-coverage-slip-detector --update", // Used to 'ratchet' up coverage after improving it.
"test:setCoverageExceptionsBaseline": "jest-a-coverage-slip-detector --force-update" // Sets the baseline for test coverage (accepts any under-target coverage).
}
}
If you're happy with the defaults below, nothing further is needed:
{
"coverageGoal": { "lines": 80, "functions": 80, "statements": 80, "branches": 80 },
"coverageGlob": [
"**/*.{ts,tsx,js,jsx}",
"!**/node_modules/**",
"!**/vendor/**",
]
}
Otherwise:
.jest-a-coverage-slip-detector
directory in the root of your projectconfig.json
file within the .jest-a-coverage-slip-detector
directoryExample:
{
"coverageGoal": { "lines": 90, "functions": 90, "statements": 90, "branches": 90 },
"coverageGlob": ["./app/javascript/**/*.{ts,tsx,js,jsx}"]
}
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:
TESTFILES=$(npx jest --listTests | sed s:$PWD/:: | circleci tests split --split-by=timings --show-counts)
npm 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:// example
COVERAGE_REPORT_SHARD=coverage/coverage-final${CIRCLE_NODE_INDEX}.json
npm 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):
{
...
"mergeCoveragePath": "coverage",
...
}
$ jest-a-coverage-slip-detector --help
Usage: jest-a-coverage-slip-detector [options]
Options:
--help, -h Show this help
--update Update exceptions with improved coverage levels.
Used to 'ratchet' up coverage after improving it.
--force-update Record current coverage errors as exceptions.
Used to:
- Snapshot current coverage errors as legacy exceptions.
- Force accept a reduction in coverage.
--merge Merges together concurrently collected coverage
--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.FAQs
Ensures that new files have jest coverage and that legacy files only increase
We found that @jobber/jest-a-coverage-slip-detector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 16 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.