Add the CRAP gate
canker reads a coverage report, so the tests must write one first.
-
Install it:
Terminal window pnpm add -D @oakgreencc/canker -
Make the tests write coverage. With Node’s test runner:
Terminal window mkdir -p coverage && node --test --experimental-test-coverage \--test-reporter=spec --test-reporter-destination=stdout \--test-reporter=lcov --test-reporter-destination=coverage/lcov.info 'test/*.test.mjs'The reporter does not create the directory. With vitest, run
vitest run --coveragewithjsonamongcoverage.reporter. -
Add
canker.jsonat the project root:{ "maxCrap": 30 } -
Run the tests, then:
Terminal window npx cankerExit 0 is clean, 1 lists offenders, 2 is a usage or input error.
Adopt it with existing offenders
Section titled “Adopt it with existing offenders”canker --init # freeze today's offenders in canker-baseline.jsonCommit the baseline. New offenders, and baselined functions that get worse, still fail. When scores improve, run canker --update and commit: it only ever lowers or removes entries.
Run it in Nx
Section titled “Run it in Nx”Give test its coverage output and add a cached crap target that depends on it, in the project’s package.json:
"nx": { "targets": { "test": { "outputs": ["{projectRoot}/coverage"] }, "crap": { "command": "canker", "options": { "cwd": "{projectRoot}" }, "dependsOn": ["test"], "cache": true, "inputs": ["default", "^production", { "externalDependencies": ["@oakgreencc/canker"] }] } }}Then add crap to the verify command, e.g. nx affected -t lint test crap. To cover every project, define the target once in nx.json targetDefaults instead.
Config keys and scoring: canker reference. Why: Quality ratchets.