unary-operator
-total total This mutator removes a unary -, + or ~ as if it had been forgotten:
-total becomes total, and +input becomes input.
Tests catch these mutants with a value that the operator changes, such as a total other than 0.
The mutant drops the operator and keeps the code after it as written:
| Code | Mutant |
|---|---|
|
total |
|
1 |
|
input |
|
GIFT_WRAP |
|
(quantity / perBox) |
Mootant removes a + only where the value after it may be something other than
a number, such as a string, null or a value of unknown type. It removes ~~,
which drops the fraction of a number, as a whole.
A -1 returned by a comparator written inside a sort or toSorted call gets
no mutant from this mutator; see Skipped changes.
Check the result with values for which the code and the mutant differ. Take this function:
export function hasCoupon(codes: string[], code: string): boolean {
return codes.indexOf(code) !== -1
return codes.indexOf(code) !== 1
}A test that looks up the first code of the list does not catch → -11:
indexOf returns 0, and both 0 !== -1 and 0 !== 1 are true. Look up a code
that is not in the list:
it('rejects a code that is not in the list', () => {
expect(hasCoupon(['SALE10'], 'WELCOME')).toBe(false)
})With the mutant, hasCoupon(['SALE10'], 'WELCOME') returns true: indexOf
returns -1, and -1 !== 1 is true.
Values that tell the other changes apart:
→-totaltotal: a total other than 0 andNaN.→+inputinput: a string, such as'3', and an assertion that expects 3.→~GIFT_WRAPGIFT_WRAPinoptions & ~GIFT_WRAP: a flag set inoptions.~~removed: a quotient with a fraction, such as 10 items in boxes of 4.
No test catches → +inputinput in +input * 2: the * converts the string
too. Ignore such a mutant with a comment.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'unary-operator': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line unary-operator: the subtraction converts the date
let started = +new Date()
let elapsed = Date.now() - startedThis 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 |
|---|---|
-1→1 in sort((a, b) => …) | number-literal turns -1 into 0 insteadownedcomparator-sign-flip |
+price→price for price: number | Value is a number by its typeequivalentnumeric-plus |
-1→1 in setTimeout(retry, -1) | Delay of a timer, made of literalsnoisetimer-delay |
+price→price in price = +price | May keep a loop running forevertimeoutloop-advance |
+'5'→'5' | Converts a literal that is not a numbernoiseliteral-coercion |
+5→5 | + doesn’t change a number literalequivalentnumber-literal-plus |
The timeouts setting, on in strict, brings
back loop-advance. The other changes stay skipped.
Negations, such as !coupon, belong to condition.