Write a mutation test run with Stryker
Run StrykerJS with the Vitest runner to change your source on purpose and find which tests still pass when they should fail.
Coverage tells you a line ran, not that a test would notice if it were wrong. Mutation testing changes the code and checks whether any test fails.
export function discount(qty) {
return qty >= 10 ? 0.9 : 1;
}import { test, expect } from 'vitest';
import { discount } from './discount.js';
test('bulk orders get 10% off', () => {
expect(discount(20)).toBe(0.9);
expect(discount(1)).toBe(1);
});{
"testRunner": "vitest",
"mutate": ["src/**/*.js", "!src/**/*.test.js"],
"reporters": ["clear-text"]
}Install @stryker-mutator/core and @stryker-mutator/vitest-runner, then run npx stryker run. One of the reported mutants, trimmed:
[Survived] EqualityOperator
src/discount.js:2:10
- return qty >= 10 ? 0.9 : 1;
+ return qty > 10 ? 0.9 : 1;Stryker rewrote >= to > and the tests still passed, because nothing tests a quantity of exactly 10. Adding expect(discount(10)).toBe(0.9) kills that mutant. The operators it applies are listed in the Stryker mutator docs.
A full run executes the tests once per mutant, so point mutate at the code that matters, or run it nightly instead of on every push.