arithmetic-operator
price * quantity price / quantity This mutator swaps the operator of a calculation, as if the code used the wrong
one: price * quantity becomes price / quantity. When a + may join strings,
it drops the right side instead, so name + sku becomes name.
Tests catch these mutants with values for which the two versions differ, such as a price of 10 and a quantity of 3.
Each operator becomes one other operator:
| Code | Mutant |
|---|---|
subtotal |
subtotal - shipping |
paid |
paid + total |
price |
price / quantity |
total |
total * count |
items |
items * perBox |
side |
side * 2 |
Mootant swaps + for - only when one side is known to be a number, such as a
number parameter or a numeric literal, and the other side is not known to be
anything else. In plain JavaScript, total + 1 still becomes total - 1.
Any other + may join strings, so Mootant drops its right side:
| Code | Mutant |
|---|---|
name, both strings |
name |
a in JavaScript with no types |
a |
Error messages get no drop, and a string literal on the right, such as
' (sale)', is left to string-literal; see
Skipped changes.
Check the result with values for which the two versions differ. Take this function:
export function lineTotal(price: number, quantity: number): number {
return price * quantity
return price / quantity
}A test that buys one item does not catch price →
* quantityprice / quantity, because 10 * 1 and 10 / 1 are both 10. Buy more than
one:
it('multiplies the price by the quantity', () => {
expect(lineTotal(10, 3)).toBe(30)
})With the mutant, lineTotal(10, 3) returns about 3.33 instead of 30.
Values that tell the other changes apart, for finite numbers:
+and-: a right side other than 0.*and/: a left side other than 0 and a right side other than 1 and -1.items→%perBoxitems * perBox: whole numbers with a left side other than 0. Fractions may give the same result:0.5 % 1and0.5 * 1are both 0.5.side→**2side * 2: a side other than 0 and 2. With 3,side ** 2is 9 andside * 2is 6.- A dropped right side, such as
name→+ skuname: a non-empty string or a number other than 0 on the right, and an assertion on the whole result.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'arithmetic-operator': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line arithmetic-operator: random jitter
let delay = base + Math.random() * 100This mutator has no options.
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 |
|---|---|
'Unknown ' + id→'Unknown ' | Text of an error message, such as in new Error()noiseerror-message |
1000 * 60→1000 / 60 | Delay of a timer, made of literalsnoisetimer-delay |
index + 1→index - 1 | May keep a loop running forevertimeoutloop-advance |
price * 1→price / 1 | 1 or -1 on the right gives the same resultequivalentidentity-operand |
price + 0→price - 0 | 0 on the right of a number gives the same resultequivalentnumeric-identity |
name + ' (sale)'→name | string-literal empties the string instead, except in a timer delayownedO11 |
price - -discount→price + -discount | unary-operator removes the - insteadownedO14 |
base + sep→base in base + sep + sep | Same code as another mutantduplicateduplicate |
The timeouts setting, on in strict, brings
back loop-advance. The other changes stay skipped.
Compound assignments, such as total += price, and updates, such as count++,
belong to assignment-operator and
update-operator.