Mutation Testing: Measuring Test Strength
Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.
What you will be able to do
#- Explain what a mutation is and what a surviving mutant reveals.
- Interpret a mutation score and its limits.
- Strengthen a weak test using a surviving mutant as the clue.
Overview
#Coverage counts lines, not checks
Line coverage tells you a line ran. It does not tell you whether any assertion would notice if that line were wrong. A test can execute a function, assert nothing meaningful, and still report full coverage. Mutation testing measures something closer to what people think coverage measures.
What a mutation is
A mutation tool makes a small change to the source, such as flipping > to >=, replacing + with -, or returning true instead of false. It then runs the test suite. If a test fails, the mutant is killed: the suite noticed the change. If the suite still passes, the mutant survived, which means no test distinguishes the original code from the broken version.
// original
function isAdult(age) {
return age >= 18;
}
// mutant
function isAdult(age) {
return age > 18;
}
If the suite has a test for isAdult(18), the mutant is killed. If the suite only tests isAdult(25), the mutant survives, and the boundary is untested.
The mutation score
The mutation score is killed mutants divided by total mutants. A high score means the suite is sensitive to small changes. A low score means large parts of the code are covered but not checked. The score is a diagnostic, not a target to maximize; some mutants are equivalent to the original and cannot be killed.
Equivalent mutants
Some mutations do not change behavior, so no test can kill them. Renaming a local variable or reordering independent statements produces an equivalent mutant. Chasing a perfect score means writing tests for behavior that does not exist. Read surviving mutants and judge each one.
Use it on critical code
Mutation testing is slow because it reruns the suite once per mutant. Run it on the modules where correctness matters most: pricing, permissions, validation, scheduling. A surviving mutant in a permission check is a security signal; a surviving mutant in a log formatter is not worth a test.
Capability note
Mutation testing runs the real test suite many times, so it needs the same runtime as the tests. It runs in a Node or CI environment, not inside a browser sandbox that cannot spawn the runner.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
The suite only tests `isAdult(25)`. What happens to the mutant `age > 18`?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptfunction isAdult(age) { return age >= 18; } // suite: assertEqual(isAdult(25), true, 'adult')Exercise 2Explain
Explain why full line coverage can coexist with weak tests.
Reveal the reference answer
reference.javascriptLine coverage only records that a line executed. A test can call a function and assert nothing, or assert something unrelated, and the line still counts as covered. Mutation testing exposes this because it changes the line and checks whether any assertion fails. If no test fails, the line was covered but not checked, so the coverage number overstated the suite's strength.The answer should separate execution from assertion and connect surviving mutants to unchecked behavior.
Exercise 3Implement
Implement `mutationScore(killed, total)` that returns the percentage rounded to one decimal, or 0 when total is 0. `console.log(mutationScore(9, 10), mutationScore(0, 0))` should print `90 0`.
starter.javascriptfunction mutationScore(killed, total) { // your code here }Reveal the reference answer
reference.javascriptfunction mutationScore(killed, total) { if (total === 0) return 0; return Math.round((killed / total) * 1000) / 10; }Authored expected output: 90 0
The guard prevents division by zero. Multiplying by 1000, rounding and dividing by 10 gives one decimal place, so 9/10 becomes 90.
Exercise 4Debug
This test claims to cover `discount` but a mutant that removes the discount still passes. Strengthen the test so the mutant is killed.
starter.javascriptfunction discount(price, rate) { return price - price * rate; } const result = discount(100, 0.1); if (typeof result === 'number') { console.log('pass'); }Reveal the reference answer
reference.javascriptfunction discount(price, rate) { return price - price * rate; } const result = discount(100, 0.1); if (result === 90) { console.log('pass'); } else { throw new Error('expected 90 but got ' + result); }Authored expected output: pass
The original only checks the type, which stays 'number' whether or not the discount is applied. Asserting the exact value 90 fails when the discount is removed, so the mutant is killed.
Exercise 5Test
Write tests for `isAdult` that kill both the `>=` to `>` mutant and the `>=` to `<=` mutant.
starter.javascriptfunction isAdult(age) { return age >= 18; }Reveal the reference answer
reference.javascriptfunction assertEqual(actual, expected, label) { if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual); } assertEqual(isAdult(17), false, 'below boundary'); assertEqual(isAdult(18), true, 'on boundary'); assertEqual(isAdult(19), true, 'above boundary'); console.log('isAdult mutants killed');Authored expected output: isAdult mutants killed
The boundary value 18 kills the `>` mutant because it expects true while the mutant returns false. The value 17 kills the `<=` mutant. The three tests pin the exact boundary behavior.
Exercise 6Interview
In an interview, explain when you would run mutation testing and what you would do with the results.
Reveal the reference answer
reference.javascriptI run it on modules where a silent error is costly: permissions, pricing, validation and state machines. I read the surviving mutants rather than chasing a score. A surviving mutant in a permission check tells me a security-relevant branch is untested, so I add a test. A surviving mutant from an equivalent change tells me to ignore it. I keep mutation testing off the default fast path because it reruns the suite once per mutant and is slow.The answer should target high-value code, treat the score as a diagnostic, and account for equivalent mutants and runtime cost.
Common misconceptions
#- One hundred percent line coverage means the tests are strong.
- Every surviving mutant must be killed to have good tests.
- Mutation testing replaces normal test runs.
Follow-up practice
#- testing-flaky-and-deterministic
- testing-capstone-quality-task-board
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.