On this page

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.

Changes

The mutant drops the operator and keeps the code after it as written:

Code Mutant
-total total
-1 1
+input input
~GIFT_WRAP GIFT_WRAP
~~(quantity / perBox) (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.

Kill a surviving mutant

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 -1 → 1: 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:

  • -total → total: a total other than 0 and NaN.
  • +input → input: a string, such as '3', and an assertion that expects 3.
  • ~GIFT_WRAP → GIFT_WRAP in options & ~GIFT_WRAP: a flag set in options.
  • ~~ removed: a quotient with a fraction, such as 10 items in boxes of 4.

No test catches +input → input in +input * 2: the * converts the string too. Ignore such a mutant with a comment.

Configuration

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() - started

This mutator has no options.

Skipped changes

Mootant leaves out these changes. npx mootant list src --skipped shows them with the category and the rule; Skipped changes explains the categories.

ChangeReason
-1→1 in sort((a, b) => …)number-literal turns -1 into 0 insteadownedcomparator-sign-flip
+price→price for price: numberValue 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 = +priceMay 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.