method-call
Math.max(total, 0) Math.min(total, 0) This mutator swaps a method for its opposite, as if the code called the wrong
one of a pair: Math.max(total, 0) becomes Math.min(total, 0). It also
removes a call such as trim() or filter(), as if the code forgot it:
input.trim() becomes input.
Tests catch such a mutant with a value for which
the two versions differ, such as a total above 0 for Math.max(total, 0).
This mutator changes only the methods in the tables below. It changes a string method only on a value that may be a string, and an array method only on a value that may be an array. A value of unknown type may be either, such as a parameter in plain JavaScript or a value of a type imported from another file.
Mootant swaps max, min, floor and ceil only on the global Math. With
the anyReceiver option, on in strict, it changes the methods
of both tables on any value.
Mootant swaps each method for the other one of its pair, in both directions:
| Code | Mutant |
|---|---|
code. |
code.endsWith('SHIP') |
label. |
label.toUpperCase() |
label. |
label.toLocaleUpperCase() |
name. |
name.trimEnd() |
sku. |
sku.padEnd(6, '0') |
items. |
items.some(isInStock) |
Math. |
Math.min(total, 0) |
Math. |
Math.ceil(price) |
Mootant replaces the call with the value it is called on:
| Code | Mutant |
|---|---|
input |
input |
name |
name |
title |
title |
code |
code |
card |
card |
items |
items |
items |
items |
items |
items |
items |
items |
orders |
orders |
orders |
orders |
In an optional chain, the ?. of a removed call moves to the next link:
value?.trim().length becomes value?.length. At the end of the chain, it goes
with the call: value?.trim() becomes value.
The loggers setting doesn’t skip calls inside
a log message: console.log(name.trim()) still gets name → .trim()name.
Mootant makes no case swap where the result is checked against a literal in the
same letter case, such as size.toUpperCase() === 'XL'; see
Skipped changes.
Check the result with a value that the call changes, or that the two methods of a pair treat differently. Take this function:
interface Item {
name: string
inStock: boolean
}
export function availableItems(items: Item[]): Item[] {
return items.filter(item => item.inStock)
return items
}The mutant items → .filter(item => item.inStock)items survives a test in
which every item is in stock, since the filter then keeps the whole list. Add an
item that is out of stock:
it('leaves out items that are out of stock', () => {
let shirt = { name: 'Shirt', inStock: true }
let socks = { name: 'Socks', inStock: false }
expect(availableItems([shirt, socks])).toEqual([shirt])
})With the mutant, availableItems([shirt, socks]) returns both items.
Values that tell some other changes apart:
startsWithandendsWith:'SHIP10', which has'SHIP'at one end only.everyandsome: a list where some items pass the check and others don’t.Math.maxandMath.min: arguments that differ, none of themNaN.- Case swaps: a text with cased letters, such as
'Sale', and an assertion on the whole result. - A removed call: a value that the call changes, such as
' 42 'fortrim().
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'method-call': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line method-call: only the speed depends on it
let workers = Math.max(cores - 1, 1)Your setting for this mutator replaces the one from strict: with strict,
'method-call': 'on' turns anyReceiver back off.
false by default, true in strict. With true, Mootant changes these
methods on any value, such as filter() of your own class or _.max():
stock.filter('A1') becomes stock.
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'method-call': ['on', { anyReceiver: true }],
},
})Mootant leaves out these changes. npx mootant list src --skipped shows them
with the category and the rule; Skipped changes
explains the categories.
| Change | Reason |
|---|---|
stock.filter('A1')→stock | Method of another type, such as your own classnoisereceiver-type |
note.trimEnd()→note.trimStart() in if (…) | Only the truth of the result countsequivalenttruth-keeping-swap |
size.toUpperCase()→size.toLowerCase() in … === 'XL' | comparison-operator or condition makes the check constant insteadownedO17 |
code.slice(0)→code | Call returns the whole stringequivalentidentity-call |
size.toUpperCase()→size.toLowerCase() in … === '42' | Compared text has no cased lettersequivalentcaseless-literal |
id.padStart(8, '0')→id.padEnd(8, '0') before .length | Only the length of the result countsequivalentlength-keeping-swap |
code.slice(1)→code in code = code.slice(1) | May keep a loop running forevertimeoutloop-advance |
code.trim()→code in code.trim().trim() | Same code as another mutantduplicateduplicate |
super.filter(isPositive)→super | Mootant can’t switch a mutant on hereinvalidinvalid-target |
The anyReceiver option brings back receiver-type, and the
timeouts setting brings back loop-advance;
strict turns both on. The other changes stay skipped.