Skip to content
Mutation Testing — Explained with Examples

Mutation Testing — Explained with Examples

DodaTech Updated Jun 15, 2026 2 min read

Mutation testing evaluates test quality by introducing small bugs (mutations) into code and checking whether existing tests detect and fail on them.

Mutation testing goes beyond code coverage. While coverage tells you what code ran, mutation testing tells you whether your tests actually verify behavior. It measures the quality of your tests, not just their reach.

How Mutation Testing Works

  1. Run all tests against the original code (baseline)
  2. Create mutants — slightly modified versions of the code (e.g., change > to <, + to -, flip a boolean)
  3. Run the test suite against each mutant
  4. If tests pass on a mutant → survived (tests didn’t catch the change — bad)
  5. If tests fail on a mutant → killed (tests caught the change — good)
// Original code
function isEven(n) {
  return n % 2 === 0;
}

// Mutants created by Stryker (mutation testing tool):
// Mutant 1: n % 2 !== 0   (flipped operator)
// Mutant 2: n % 2 === 1   (changed expected value)
// Mutant 3: n % 3 === 0   (changed divisor)
// Mutant 4: false         (replaced return with constant)

// A good test should KILL all these mutants:
test('returns true for even numbers', () => {
  expect(isEven(2)).toBe(true);
  expect(isEven(0)).toBe(true);
});

test('returns false for odd numbers', () => {
  expect(isEven(1)).toBe(false);
  expect(isEven(3)).toBe(false);
});

Mutation Score

Mutation score = (Killed mutants / Total mutants) × 100

If 80 mutants are created and tests kill 72:
Score = (72 / 80) × 100 = 90%

Target: 80%+ for meaningful test quality.

Running Mutation Tests (Stryker)

npx stryker run
Mutant report:
#1. [Survived] isEven → replaced === with !==
  Tests: [none]
  This means your tests don't check the actual comparison
#2. [Killed]  isEven → replaced 0 with 1
  Tests: [test returns false for odd numbers]

Real-World Analogy

Code coverage is checking that every room in a building has a smoke detector. Mutation testing is actually blowing smoke into each room to see if the alarm goes off. You might have a smoke detector in every room (100% coverage), but if the batteries are dead, the alarms won’t sound when smoke appears (mutants survive).

Related Terms

Code Coverage, Unit Testing, TDD, Regression Testing

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro