On this page

condition

if (cart.coupon) if (true)

This mutator makes a condition always true or always false: if (cart.coupon) becomes if (true) in one mutant and if (false) in another. The true mutant stands for a missing check, and the false mutant for a branch that never runs.

Tests catch them with an input for each outcome, such as a cart with a coupon and one without, and a check of a result that depends on the condition.

Changes

Mootant forces these conditions to true or false:

Code Mutants Where
if (cart.coupon) if (true) and if (false) Test of an if
cart.express ? 15 : 5 true ? 15 : 5 and false ? 15 : 5 Test of a ternary
while (page.length) while (false) Test of a loop
!product.inStock true and false A ! outside a test
for (;;) for (;false;) A for loop without a test

A loop test also becomes true with the timeouts setting, on in strict, since true may keep the loop running forever, whatever else can end the loop. false in an if that leaves an endless loop, such as if (done) break in while (true), also needs the setting, unless another break, continue, return or throw can leave each endless loop that the if leaves.

A comparison, a && or || group or a literal, also with ! in front, gets no mutant of this mutator, and neither does a ! on a side of && or ||. Their own mutators change them instead: comparison-operator, logical-operator and the literal mutators, such as boolean-literal. When comparison-operator is off or ignored, a comparison can get mutants of this mutator; see Skipped changes.

Kill a surviving mutant

Test the code once with the condition true and once with it false, and check a result that depends on it. Take this function:

export function cartTotal(subtotal: number, coupon?: string): number {
  if (coupon) {
  if (true) {
    return subtotal - 10
  }
  return subtotal
}

Tests that always pass a coupon do not catch if (coupon) → if (true): with a coupon, the condition is already true. A test without a coupon catches it:

it('charges the full subtotal without a coupon', () => {
  expect(cartTotal(50)).toBe(50)
})

With the mutant, cartTotal(50) returns 40 instead of 50.

Tests that catch the other changes:

  • coupon → false: a test with a coupon, such as expect(cartTotal(50, 'SAVE10')).toBe(40).
  • A loop test → false and for (;;) → for (;false;): a test in which the loop body runs and changes the result. A do...while body must run at least twice.
  • !product.inStock → true and false in a filter() callback: a list with one product in stock and one sold out, and a check of the whole result.

Configuration

Turn the mutator off in the configuration:

import { defineConfig } from 'mootant'

export default defineConfig({
  mutators: {
    condition: 'off',
  },
})

To ignore the mutants of this mutator on one line, put an ignore comment above it:

// mootant-ignore-next-line condition: dev-only warning
if (import.meta.env.DEV) {
  warnAboutPrices(cart)
}

Turning this mutator off or ignoring it doesn’t bring back the changes that rule O19 skips: changes of other mutators that do the same as its true or false on a condition without an effect. For example, statement-removal still doesn’t remove applyCoupon(order) in if (order.coupon) applyCoupon(order), so that line gets no mutant at all.

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
code === 'SALE'→trueComparison, group or literal, left to its own mutatorownedO1
stockCount > 0→truecomparison-operator gives this value on some inputssubsumedcomparison-region
!isMember→falseSide of && or ||, left to logical-operatorownedO3
page.length→true in while (page.length)Makes a loop condition always truetimeoutloop-running
!hasMore()→false in done = !hasMore()May keep a loop running forevertimeoutloop-advance
true→false in while (true)Literal test, left to its mutator, such as boolean-literalownedO7
stock.isEmpty()→true at the start of for (;;)Same as making the loop test falseownedO13
products→true for products: Product[]Type hints show the condition already has this valueequivalenttyped-constant
express ?? false→trueAnother ?? mutant gives this value on some inputssubsumedR3
a.featured→false in sort()method-call removes the sort() call insteadownedO12
typeof sku === 'string'→trueType hints show the comparison already has this resultequivalentproven-comparison
cart.items.length >= 0→trueSame result, assuming a lower limit, such as 0 for a lengthequivalentlower-bound
1→true in if (1)The literal already has this truth valueequivalentliteral-truth
1→false in if (1)Literal test, taken as written on purposenoiseconstant-test

The timeouts setting, on in strict, brings back loop-running and loop-advance. R3 and comparison-region come back when the other mutant doesn’t run, for example because its mutator is off. The other changes stay skipped.

A returned boolean, such as return product.inStock, belongs to return-value.