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.
- arithmetic-operator
price*quantityprice / quantitySwaps an arithmetic operator or drops the right side of a +.
- array-element
[subtotal,shipping,tax][subtotal, tax]Removes one array element, or puts undefined into an empty array.
- assignment-operator
total+=shippingtotal -= shippingSwaps a compound assignment operator or changes when it assigns.
- bitwise-operator
flags&maskflags | maskSwaps a bitwise or shift operator, or drops a | 0.
- boolean-literal
let freeShipping =falselet freeShipping = trueFlips true to false and false to true.
- comparison-operator
total>=50total > 50Moves the boundary of a comparison or swaps its operator.
- condition
if (cart.coupon)if (true)Forces a condition to true or false.
- logical-operator
inStock &&hasAddresshasAddressDrops one side of &&, || or ??, or swaps || and ??.
- method-call
Math.max(total, 0)Math.min(total, 0)Swaps a method for its opposite or removes a call like trim().
- missing-await
awaitloadCart(id)loadCart(id)Removes an await, as if it had been forgotten.
- number-literal
FREE_SHIPPING_FROM =50FREE_SHIPPING_FROM = 51Adds one to a number.
- object-property
{name,price }{ price }Removes one property or spread from an object literal.
- optional-chaining
coupon?.codecoupon.codeReplaces ?. with a plain property access or call.
- regex-flag
/^save\d+$/i/^save\d+$/Removes a flag of a regular expression.
- regex-pattern
/^\d{5}$//\d{5}$/Changes one part of a regular expression pattern.
- return-value
cart.items.length0Replaces a returned value with a fixed value picked by its type.
- statement-removal
orders.push(order);Removes one statement that has an effect, such as a call.
- string-literal
'EUR'''Empties a string or fills an empty one.
- unary-operator
-totaltotalRemoves the -, + or ~ in front of a value.
- update-operator
count++count--Swaps ++ and --, or prefix and postfix when the value is used.
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.
Mootant leaves out some changes. To see them, add --skipped:
npx mootant list src --skippedEach 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 --jsonmarks 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:
timeoutskeeps thetimeoutchanges, andcoarseMutantsthe coarse mutants.strictturns both on.loggersand some mutator options bring back other changes.- Most
subsumedchanges come back when the other mutant does not run, for example because its mutator is off.ownedchanges 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.
Set the mutator to 'off' in mutators of the
configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'string-literal': 'off',
},
})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-startandmootant-ignore-endcover the lines between them.mootant-ignore-filecovers 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 */}.