number-literal
FREE_SHIPPING_FROM = 50 FREE_SHIPPING_FROM = 51 This mutator adds one to a number, as if the code were off by one:
FREE_SHIPPING_FROM = 50 becomes FREE_SHIPPING_FROM = 51.
Tests catch these mutants with inputs for which the two numbers give different results, such as an order total of exactly 50.
Each number goes up by 1:
| Code | Mutant |
|---|---|
FREE_SHIPPING_FROM = |
FREE_SHIPPING_FROM = 51 |
TAX_RATE = |
TAX_RATE = 1.2 |
REFUND = - |
REFUND = -4 |
items[items.length - |
items[items.length - 2] |
Math.min(stock, |
Math.min(stock, 11) |
price.toFixed( |
price.toFixed(3) |
PRICE_CENTS = |
PRICE_CENTS = 2000n |
Decimals go up by a whole 1 too: 0.2 becomes 1.2. A minus sign that makes a
number negative counts as part of it, so -5 becomes -4, not -6. In
items[items.length - 1], the 1 becomes 2, so the mutant reads the item before
the last one.
Mootant skips some numbers in common code, such as timer delays and case
labels. It leaves the numbers of a comparison, such as 10 in quantity > 10,
to comparison-operator. See
Skipped changes.
Check the exact result with input for which the old and the new number give different results. Take this function, which gives 10% off with a cap of 20:
export function couponDiscount(total: number): number {
return Math.min(total * 0.1, 20)
return Math.min(total * 0.1, 21)
}A test that expects a discount of 10 for an order of 100 catches → 0.11.1.
It does not catch → 2021: a discount of 10 stays under the cap, so both
versions return 10. Test an order whose discount goes over the cap:
it('caps the discount at 20', () => {
expect(couponDiscount(500)).toBe(20)
})With the mutant → 2021, couponDiscount(500) returns 21 instead of 20.
Numbers in other places need other tests:
- An index, such as
items.length - 1: a list whose items differ. - A default value, such as
quantity = 1: a call without that argument. - A rate, such as
TAX_RATE = 0.2: an amount such as 100, not 0. - Decimal places, such as
toFixed(2): the whole string, checked withtoBe. - A
caselabel, withcaseTests: the value of the label, such as1.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'number-literal': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line number-literal: analytics sampling
export const SAMPLE_RATE = 0.1When this mutator is off, or its mutants on a line are ignored, a coarser mutant
can come back, such as → return 0; from
statement-removal.
['on', options] merges the options over the
defaults of the mutator, not over the options
of the preset: with strict, 'number-literal': 'on' turns caseTests back
off.
false by default, true in strict. With true, Mootant also changes the
numbers of case labels, such as → 12 in case 1:.
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'number-literal': ['on', { caseTests: 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 |
|---|---|
10→11 in quantity >= 10 | Literal in a comparison, left to comparison-operatorsubsumedO4 |
10→11 in quantity > 10 | comparison-operator moves the number by one insteadownedshifted-literal |
1000→1001 in setTimeout(run, 1000) | Delay of a timer, made of literalsnoisetimer-delay |
1→2 in case 1: | Value of a case labelnoisecase-label |
1→2 in express ? 1 : 2 | Same result as a true or false of another mutator, for a condition without an effectownedO19 |
1→2 in quantity > 6 ? 2 : 1 | Copies the other branch, as comparison-operator does at the boundary, with the same effectssubsumedR5 |
1→2 in a.price > b.price ? 1 : -1 | Same sign, and sort() reads only the signequivalentcomparator-sign |
-1→0 in sku.slice(-1) | method-call removes the slice() call insteadownedO18 |
-1→0 in items.slice(-1) | method-call removes the slice() call, which also gives every itemsubsumedslice-copy |
0→1 in void 0 | Operand of void, whose value is always undefinedequivalentvoid-operand |
0→1 in filter(product => product.stock || 0) | Side of && or ||, left to logical-operatorownedO3 |
1→2 in { [1]: 'Standard' } | Key of an object or a classnoiseproperty-key |
1→2 in while (1) | Same truth value in a conditionequivalentsame-truth |
0→1 in do { … } while (0) | Makes a loop condition always truetimeoutloop-running |
0→1 in attempts = 0 inside while (attempts > 0) | May keep a loop running forevertimeoutloop-advance |
-1→0 in return -1 | method-call removes the sort() call insteadownedO12 |
-1→0 in stock || -1 | logical-operator drops the default insteadownedO16 |
-1→0 in limit || -1 | logical-operator swaps || for ?? insteadsubsumedR7 |
The timeouts setting brings back
loop-running and loop-advance, and caseTests brings back
case-label; strict turns both on. O4, R5, R7 and slice-copy come
back when the other mutant does not run, for example when its mutator is off.
The other changes stay skipped.