patterntypescriptnoneTip
Mutation Testing with Stryker for Meaningful Coverage
Viewed 0 times
mutation testingStrykermutation scoretest qualitykilled mutantsurviving mutant
Problem
100% code coverage does not guarantee tests catch bugs. Tests that run code without asserting results will have perfect coverage but detect nothing.
Solution
Use Stryker mutation testing to verify that test assertions actually catch code changes.
# Install
npm install --save-dev @stryker-mutator/core @stryker-mutator/jest-runner
# stryker.config.json
{
"testRunner": "jest",
"reporters": ["html", "clear-text", "progress"],
"coverageAnalysis": "perTest",
"mutate": ["src/**/*.ts", "!src/**/*.test.ts"]
}
# Run mutation testing
npx stryker run// Stryker mutates your code (e.g., changes + to -) and runs tests
// If tests still pass after the mutation, it's a 'surviving mutant'
// Surviving mutants = test gaps
// Example mutation:
function add(a: number, b: number): number {
return a + b; // Stryker mutates to: return a - b;
}
// Test must assert the exact result:
test('add returns correct sum', () => {
expect(add(2, 3)).toBe(5); // Catches the mutation
// NOT: expect(add(2, 3)).toBeDefined(); // Misses the mutation
});
// Mutation score = (killed mutants / total mutants) * 100
// Aim for 70-80%+ for critical business logicWhy
Mutation testing introduces deliberate bugs (mutations) and checks if tests catch them. A high mutation score means your tests are actually sensitive to behavior changes, not just coverage.
Gotchas
- Mutation testing is slow — run it on CI nightly, not on every commit.
- Some mutants are 'equivalent' — semantically the same as the original — and cannot be killed. Exclude them.
- Focus mutation testing on business-critical code rather than the entire codebase.
Revisions (0)
No revisions yet.