On this page

Mutators

A mutator makes one kind of change to your code, such as + to -. Each mutant in a report names the mutator that made it. The page of a mutator shows its changes, a test that catches them and the changes it skips.

Mootant has 20 built-in mutators, and both presets turn all of them on. strict also turns on all mutator options but jsxAttributes, the mutants that may cause timeouts and the coarse mutants that recommended drops.

Surviving mutants

A surviving mutant means that the tests did not detect its change. Before you write a test, check that the change matters for the inputs your code can get. The page of each mutator shows which inputs tell the two versions apart.

Some changes affect nothing a test can see, such as attempts++ → ++attempts in setTimeout(() => attempts++). Ignore such a mutant with a comment.

Skipped changes

Mootant leaves out some changes. To see them, add --skipped:

npx mootant list src --skipped

Each skipped change comes with its rule, a reason and one of these categories:

CategoryThe change
equivalentCan’t alter what the code does, apart from details such as undefined for null
noiseTouches something Mootant leaves untested on purpose, such as a log message
timeoutAlters a value that decides whether a loop goes on, so it may loop forever
ownedHas the same effect as a mutant that Mootant makes instead
subsumedIs caught by any test that catches another mutant
duplicateGives the same code as an earlier mutant
invalidWould not compile, or sits where Mootant can’t switch a mutant on

A category is Mootant’s decision, not a proof about your code:

  • Some rules rest on a guess, such as a type hint or a method name. npx mootant list src --skipped --json marks their changes with "heuristic": true.
  • For a coarse mutant, rule solitary, Mootant assumes that a test that catches a finer mutant inside also catches the removal. That is likely, not certain.
  • Mootant builds all mutants into one copy of your code and turns them on one at a time. A few places, such as the operand of delete, can’t hold that switch.

Settings that bring skipped changes back:

  • timeouts keeps the timeout changes, and coarseMutants the coarse mutants. strict turns both on.
  • loggers and some mutator options bring back other changes.
  • Most subsumed changes come back when the other mutant does not run, for example because its mutator is off. owned changes stay skipped.

With --since, Mootant also selects mutants used in place of skipped changes on changed lines. These mutants may be on unchanged lines. This keeps the checks that a full run would make for those changes.

A skip rule may identify a mutator and a location without naming a specific replacement. In that case, Mootant selects all mutants from that mutator at that location, such as both true and false from condition.

Code that Mootant never mutates, such as types, enums, decorators and module paths, doesn’t appear in the list at all.

Turn a mutator off

Set the mutator to 'off' in mutators of the configuration:

import { defineConfig } from 'mootant'

export default defineConfig({
  mutators: {
    'string-literal': 'off',
  },
})

Ignore mutants

To ignore the mutants of a mutator on one line, put an ignore comment with its name above the line, and the reason after a colon:

// mootant-ignore-next-line string-literal: event name
emitter.emit('change', value)

list, run --json --all and the HTML report keep ignored mutants with the reason, and the score leaves them out.

Ignoring the finer mutants of a line can bring back a coarse one. Here the line gets emitter.emit('change', value) → ; from statement-removal. To keep it out, list both mutators:

// mootant-ignore-next-line string-literal, statement-removal: event name
emitter.emit('change', value)

Other forms of the comment:

  • mootant-ignore-start and mootant-ignore-end cover the lines between them.
  • mootant-ignore-file covers the whole file.
  • Without mutator names, a comment ignores the mutants of every mutator.
  • In a Svelte or Vue template, write an HTML comment: <!-- mootant-ignore-next-line -->.
  • Between JSX elements, write the comment in braces: {/* mootant-ignore-next-line */}.