Coverage tells you which lines ran during the tests. It does not tell you whether a test would fail if those lines were wrong. I wrote three test suites for the same small module, and all three reached 100% line and branch coverage. Their mutation scores were 18.42%, 34.21% and 100%.
Chapter 1 of Mutation Testing for the AI Era. The module, the suites, the measure script and the raw reports are in the PoC repo.
Two functions that a real HTTP client needs. shouldRetry says if a status is worth another try. retryDelay gives an exponential backoff delay with a cap, and throws on an attempt out of range.
export const DEFAULTS: RetryOptions = { baseMs: 100, maxMs: 2000, maxAttempts: 5 };
// 408 and 429 are worth another try; so is a 5xx, except 501 (the server will never support the request)
export function shouldRetry(status: number): boolean {
if (status === 408 || status === 429) return true;
return status >= 500 && status < 600 && status !== 501;
}
// Exponential backoff: baseMs, 2 x baseMs, 4 x baseMs ... capped at maxMs
export function retryDelay(attempt: number, options: RetryOptions = DEFAULTS): number {
if (attempt < 1 || attempt > options.maxAttempts) {
throw new RangeError(`attempt must be between 1 and ${options.maxAttempts}, got ${attempt}`);
}
return Math.min(options.baseMs * 2 ** (attempt - 1), options.maxMs);
}
Each suite calls both functions with inputs that reach every line and every branch. They differ only in what they check.
runs-only checks that something came back. The out-of-range call sits in a try/ catch, so the test passes whether it throws or not.
it('handles retryable statuses', () => {
expect(shouldRetry(408)).toBeDefined();
expect(shouldRetry(429)).toBeDefined();
expect(shouldRetry(503)).toBeDefined();
});
type-checks checks the type and the range: a boolean, a delay above 0, and a throw on bad input.
it('returns a positive number', () => {
expect(retryDelay(1)).toBeGreaterThan(0);
expect(retryDelay(5)).toBeGreaterThan(0);
expect(retryDelay(3, { baseMs: 50, maxMs: 1000, maxAttempts: 3 })).toBeGreaterThan(0);
});
exact-values checks the exact result at each boundary (407, 408, 409, 499, 500, 501, 599, 600 and others), the delay for each attempt, the cap, and the error message.
it('doubles the delay on each attempt', () => {
expect([1, 2, 3, 4].map((attempt) => retryDelay(attempt))).toEqual([100, 200, 400, 800]);
});
All three suites pass, and Vitest's v8 coverage reports 100% of lines, statements, branches and functions for each one.
A mutation testing tool makes one small change to the code, runs the tests, and puts the change back. It does this for every change it can make: flip < to <=, replace a condition with true, swap Math.min for Math.max, empty a string. Each changed copy is a mutant.
The mutation score is the share of mutants killed. StrykerJS 10.1.1 made 38 mutants from this module, and ran each suite against all 38.
| Suite | What the tests check | Tests | Line coverage | Branch coverage | Mutation score | Mutants survived |
|---|---|---|---|---|---|---|
runs-only |
a result came back | 4 | 100% | 100% | 18.42% | 31 of 38 |
type-checks |
type, range, throws | 3 | 100% | 100% | 34.21% | 25 of 38 |
exact-values |
exact result at each boundary | 17 | 100% | 100% | 100% | 0 of 38 |
Coverage gives the same number for all three suites. The mutation score separates them.
shouldRetry: 21 of 22 mutants survived in both weak suites. Stryker replaced status === 408 || status === 429 with true, so every status, 404 included, says "retry". Both suites passed: the result is still defined, and still a boolean. The same goes for status !== 501 flipped to status === 501, and for >= 500 changed to > 500. The one mutant both suites killed empties the function body, so it returns undefined.
retryDelay: crashes count. runs-only killed 6 of the 15 retryDelay mutants without checking a value. Five made a valid call throw, such as attempt < 1 changed to attempt <= 1, and one returned undefined. A test fails when the code under it crashes, even if the test checks nothing. That is why a suite that only calls the code still scores above 0.
type-checks killed 11 of the 15 retryDelay mutants. These 4 survived:
Math.min changed to Math.max: the cap becomes a minimum, so every delay is at least maxMs.* changed to /: the delay shrinks on each attempt. attempt - 1 changed to attempt + 1: every delay is 4 times longer, up to the cap. toThrow() with no argument passes on any error.
Each of these still returns a number above 0, so "greater than 0" passes. exact-values kills all 38, because each test names the value it expects.
Every surviving mutant, with its line and its replacement, is in results/summary.md. The Stryker HTML report for each suite is in results/<suite>/mutation.html.
Stryker needs a test runner plugin. For Vitest:
npm i -D @stryker-mutator/core @stryker-mutator/vitest-runner
The PoC's stryker.config.mjs adds a SUITE switch to compare suites. Without it, the config is:
export default {
testRunner: 'vitest',
mutate: ['src/**/*.ts'],
coverageAnalysis: 'perTest',
reporters: ['clear-text', 'json', 'html'],
};
Then npx stryker run. It prints a score per file and writes the HTML report to reports/mutation/mutation.html. Start with one module where a wrong boundary costs money or pages someone, not the whole repo.
runs-only score is 18.42%, not 0%.
Limits: one small module, hand-written suites, and Stryker's default mutators. Real suites mix all three styles. Each suite ran under Stryker twice, and every mutant had the same status both times.
Coverage tells you the code ran. A mutation score tells you the tests would notice if it were wrong. Chapter 2 runs the same check on a suite an AI agent wrote for itself.
Everything above is reproducible: qa-gary-parker/poc-coverage-vs-mutation-score. Measured with StrykerJS 10.1.1 and Vitest 5.1.2.
Originally published at https://garyparker.dev/blog/coverage-vs-mutation-score