Mutation Testing in Plain English

A tool called Stryker found in two seconds that nobody had tested what happens when a customer gets a negative discount. The tests were green. The coverage number was 100%. Mutation testing is the technique that found it: it plants small fake bugs in your code, one at a time, runs your tests after each, and counts how many your tests catch. The score is the summary, and the list of bugs that got through is the work.
This is the tool version of the sabotage check in how to tell if the AI’s tests test anything. It runs on JavaScript with Stryker, and you can follow it with four tests and a five-line function.
Four words you will see: a mutant is one planted fake bug. Killed means a test failed because of it. A survivor is a mutant every test ignored. The mutation score is killed divided by total mutants.
What does it do, step by step?
The plainest example: change “10% off” to “11% off” and see whether any test notices. If none does, no test checks the number. Stryker does that for you, many times:
- It reads your code and lists small edits: flip
<to<=, swap+for-, replace a string with an empty one, make a condition always true. - For each edit, it changes the code, runs the tests, and changes it back.
- If a test fails, the mutant is killed. If every test passes, it survived.
In the scratch run for this post price.js was unchanged afterward (checked with a diff against a backup). Keep your work committed anyway.
How do I run it myself?
You need Node.js and a terminal. If you cannot run commands, skip to the prompt near the end and ask your AI to run Stryker and show you the survivors list.
Make an empty folder, then create price.js:
export function applyDiscount(price, percent) { if (percent < 0 || percent > 100) throw new Error('bad percent'); return Math.round(price * (100 - percent)) / 100;}and price.test.js:
import { it, expect } from 'vitest';import { applyDiscount } from './price.js';
it('10% off $100 is $90', () => { expect(applyDiscount(100, 10)).toBe(90);});it('10% off $19.95 is rounded to $17.96', () => { expect(applyDiscount(19.95, 10)).toBe(17.96);});it('0% and 100% off are allowed', () => { expect(applyDiscount(50, 0)).toBe(50); expect(applyDiscount(50, 100)).toBe(0);});it('more than 100% off is rejected', () => { expect(() => applyDiscount(100, 101)).toThrow();});and stryker.config.json:
{ "testRunner": "vitest", "mutate": ["price.js"], "reporters": ["clear-text"], "coverageAnalysis": "perTest"}Then install and run:
npm init -ynpm pkg set type=module scripts.test="vitest run"npm i -D vitest @stryker-mutator/core @stryker-mutator/vitest-runnernpm testnpx stryker runnpm test should show 4 passed. The Stryker run, trimmed (Stryker 10.1.1, Vitest 5.1.2, Node 22; your mutant count may differ on other versions):
[Survived] ConditionalExpressionprice.js:2:7- if (percent < 0 || percent > 100) throw new Error('bad percent');+ if (false || percent > 100) throw new Error('bad percent');
[Survived] StringLiteralprice.js:2:53- if (percent < 0 || percent > 100) throw new Error('bad percent');+ if (percent < 0 || percent > 100) throw new Error("");
All files | 85.71 | 12 killed | 2 survived14 mutants, 12 killed, 2 survivors: 85.71%. The four tests pass, and two things nobody checked are now on a list.
How do I read a survivor?
Each survivor shows the line, the original, and the edit nobody noticed. Ask one question: if the code really did that, would a user be hurt?
Survivor 1: the check percent < 0 was replaced by false. The function would accept a negative discount such as applyDiscount(100, -5) and quietly charge more. No test covers it. Fix: add a test. Put this at the end of price.test.js:
it('a negative discount is rejected with a clear message', () => { expect(() => applyDiscount(100, -5)).toThrow('bad percent');});Survivor 2: the message 'bad percent' was replaced with an empty string, because the last test only checks that something is thrown. Whether that matters is your call. If a person reads this message, test it. If it is internal, ignoring it is a legitimate answer. Most survivors are a rule nobody is testing, and a few are not: Stryker documents these as equivalent mutants, changes that do not alter behavior and cannot be killed. Do not chase 100% for its own sake.
With the new test, the same run reports 100.00 | 14 killed | 0 survived. The test above has toThrow('bad percent') rather than a bare toThrow(), which is why it kills both survivors. A bare toThrow() kills only the first and leaves the score at 92.86%.
What did the score hide?
1. A test with no assertions can still get kill credit. In the nine-test suite from the previous post, test A (applyDiscount(100, 10); and nothing else) was credited with three kills. Mutants that made the function throw crashed that test even though it asserts nothing. A test with no assertions still catches crashes, and it cannot catch a wrong answer. So read the list, not the score. (By default Stryker stops at the first failing test per mutant, so one test gets the credit; the disableBail setting shows every failing test.)
2. 100% only means the tool’s list ran out. In the previous post, one hand-made break replaced Math.round with Math.floor. The 14 mutants Stryker generated for this function did not include that swap, so no score would show it. The tool tries the edits on its menu. Your own “what if the AI got this wrong the way I am afraid of” is not on the menu, so test it by hand.
3. The score depends on what you point it at. The config says "mutate": ["price.js"]. A perfect score on one file says nothing about the others. Aim it at the code that handles money, permissions and data.
How do I make it fail a build?
Add a threshold to stryker.config.json:
"thresholds": { "high": 90, "low": 80, "break": 95 }With the first suite, the run printed Final mutation score 85.71 under breaking threshold 95, setting exit code to 1 (failure). Stryker’s thresholds default is break: null, which never fails. A realistic start is to set break at your current score and raise it over time (advice, not something measured).
What goes wrong with mutation testing?
- It is slow on a big project. It reruns tests for every mutant. With
coverageAnalysis: "perTest"the nine-test run executed about 4.5 tests per mutant on average. Stryker’s incremental mode reruns only what changed. Start with one file. - It leaves files behind if you ask for a report. The
htmlreporter writes areports/folder (confirmed in a scratch run with"reporters": ["clear-text", "html"]; clear-text writes none). Addreports/to.gitignore. Stryker also works in a.stryker-tmpfolder, cleaned by default per its docs. - Other languages have their own tools. This post ran only JavaScript with Stryker.
Can I let the AI do the fixing?
Yes, with a boundary: it reads the survivors and writes tests for the rules they reveal, and it does not change the code under test. Paste this:
Here is the output of a mutation test run. For each survivor, tell me in one sentence what behavior is not tested and whether a user would be hurt if the code did that. Then write one test per real gap, named as a full sentence with an example number, checking what a user sees. Do not change any code outside the test files. Run the mutation test again and show me the new score. List every survivor you decided not to test, and why.
Then check that it did not edit existing tests to lift the score: run git diff --stat and read what changed in the test files.
If you have not yet seen why the tests are the thing to check, start with tests for people who do not write code. For why tests should check what users see, read test behaviors, not code.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.