# 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](https://mootant.org/guide/configuration.md#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](https://mootant.org/guide/configuration.md#coarsemutants) that `recommended`
drops.

- [arithmetic-operator](https://mootant.org/mutators/arithmetic-operator.md): Swaps an arithmetic operator or drops the right side of a +. `price * quantity` → `price / quantity`
- [array-element](https://mootant.org/mutators/array-element.md): Removes one array element, or puts undefined into an empty array. `[subtotal, shipping, tax]` → `[subtotal, tax]`
- [assignment-operator](https://mootant.org/mutators/assignment-operator.md): Swaps a compound assignment operator or changes when it assigns. `total += shipping` → `total -= shipping`
- [bitwise-operator](https://mootant.org/mutators/bitwise-operator.md): Swaps a bitwise or shift operator, or drops a | 0. `flags & mask` → `flags | mask`
- [boolean-literal](https://mootant.org/mutators/boolean-literal.md): Flips true to false and false to true. `let freeShipping = false` → `let freeShipping = true`
- [comparison-operator](https://mootant.org/mutators/comparison-operator.md): Moves the boundary of a comparison or swaps its operator. `total >= 50` → `total > 50`
- [condition](https://mootant.org/mutators/condition.md): Forces a condition to true or false. `if (cart.coupon)` → `if (true)`
- [logical-operator](https://mootant.org/mutators/logical-operator.md): Drops one side of &&, || or ??, or swaps || and ??. `inStock && hasAddress` → `hasAddress`
- [method-call](https://mootant.org/mutators/method-call.md): Swaps a method for its opposite or removes a call like trim(). `Math.max(total, 0)` → `Math.min(total, 0)`
- [missing-await](https://mootant.org/mutators/missing-await.md): Removes an await, as if it had been forgotten. `await loadCart(id)` → `loadCart(id)`
- [number-literal](https://mootant.org/mutators/number-literal.md): Adds one to a number. `FREE_SHIPPING_FROM = 50` → `FREE_SHIPPING_FROM = 51`
- [object-property](https://mootant.org/mutators/object-property.md): Removes one property or spread from an object literal. `{ name, price }` → `{ price }`
- [optional-chaining](https://mootant.org/mutators/optional-chaining.md): Replaces ?. with a plain property access or call. `coupon?.code` → `coupon.code`
- [regex-flag](https://mootant.org/mutators/regex-flag.md): Removes a flag of a regular expression. `/^save\d+$/i` → `/^save\d+$/`
- [regex-pattern](https://mootant.org/mutators/regex-pattern.md): Changes one part of a regular expression pattern. `/^\d{5}$/` → `/\d{5}$/`
- [return-value](https://mootant.org/mutators/return-value.md): Replaces a returned value with a fixed value picked by its type. `cart.items.length` → `0`
- [statement-removal](https://mootant.org/mutators/statement-removal.md): Removes one statement that has an effect, such as a call. `orders.push(order)` → `;`
- [string-literal](https://mootant.org/mutators/string-literal.md): Empties a string or fills an empty one. `'EUR'` → `''`
- [unary-operator](https://mootant.org/mutators/unary-operator.md): Removes the -, + or ~ in front of a value. `-total` → `total`
- [update-operator](https://mootant.org/mutators/update-operator.md): Swaps ++ and --, or prefix and postfix when the value is used. `count++` → `count--`

## 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](#ignore-mutants).

## Skipped changes

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

```sh
npx mootant list src --skipped
```

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

| Category     | The change                                                                        |
| ------------ | --------------------------------------------------------------------------------- |
| `equivalent` | Can't alter what the code does, apart from details such as `undefined` for `null` |
| `noise`      | Touches something Mootant leaves untested on purpose, such as a log message       |
| `timeout`    | Alters a value that decides whether a loop goes on, so it may loop forever        |
| `owned`      | Has the same effect as a mutant that Mootant makes instead                        |
| `subsumed`   | Is caught by any test that catches another mutant                                 |
| `duplicate`  | Gives the same code as an earlier mutant                                          |
| `invalid`    | Would 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`](https://mootant.org/guide/configuration.md#timeouts) keeps the `timeout` changes, and
  [`coarseMutants`](https://mootant.org/guide/configuration.md#coarsemutants) the coarse mutants.
  `strict` turns both on.
- [`loggers`](https://mootant.org/guide/configuration.md#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`](https://mootant.org/guide/cli.md#shared-options), 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](https://mootant.org/mutators/condition.md).

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](https://mootant.org/guide/configuration.md#mutators):

```ts
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:

```ts
// 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](https://mootant.org/mutators/statement-removal.md). To keep it out, list both
mutators:

```ts
// 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 */}`.
