On this page

statement-removal

orders.push(order) ;

This mutator removes one statement that has an effect, such as a call, an assignment, a throw or an early return. orders.push(order) becomes ;, so the order is never added to orders.

Tests catch it by checking what the statement changes, such as the contents of orders, not only what the function returns.

Changes

Remove a statement with an effect

Each mutant replaces one statement with the empty statement ;:

Statement Example
A call, also an optional one save(order), onChange?.()
An assignment, ++ or -- cart.coupon = null
await, new or delete delete reserved[sku]
void before a call, new or await void sendReceipt(order)
throw throw new Error('Unknown country')

With the jumps option, Mootant also removes continue, yield and every break.

Remove an early return

Mootant removes a return with no value or with a value such as null, undefined, this or another literal. Without it, the function goes on and runs code meant for other input:

export function checkout(cart: Cart): Order | null {
  if (cart.items.length === 0) {
    showEmptyCart()
    return null
  }
  return createOrder(cart)
}

With return null → ;, an empty cart reaches createOrder(cart).

Remove the break of a case

Without the break that ends a case, the code falls through into the next case:

switch (coupon) {
  case 'WELCOME':
    discount = 10
    break
  case 'VIP':
    discount = 20
    break
}

With the first break → ;, the coupon 'WELCOME' gets a discount of 20.

Statements Mootant doesn’t remove

Mootant never removes a whole if, loop or block, a declaration, such as let order = createOrder(cart), or a statement without an effect, such as price + quantity. It also skips the removal of these statements:

  • The only statement of an if without else, such as the throw in if (!coupon) throw new Error('No coupon'): another mutant already makes the test false. The removal stays when that mutant would skip an effect of the test, such as the call in if (reserve(sku)). For if (order.total > 100) addGift(order), the removal comes back when comparison-operator and condition are off.
  • In recommended, a statement with a finer mutant inside, such as count++, await save(order) or return 0, unless it is a plain = assignment. Mootant assumes that a test that catches the finer mutant also catches the removal.
  • A console call whose arguments are only text, such as console.log('Order saved'). Mootant removes other log calls, such as console.log('Saved', order.id) and logger.info('Saved').

Kill a surviving mutant

Check the effect of the statement, not only what the function returns. Take this function:

export function redeem(coupon: { used: boolean; discount: number }): number {
  if (coupon.used) {
    throw new Error('Coupon already used')
  }
  coupon.used = true
  ;
  return coupon.discount
}

A test that redeems a coupon once and checks the discount does not catch coupon.used = true → ;: the function returns the same discount. Redeem it twice:

it('accepts a coupon only once', () => {
  let coupon = { used: false, discount: 5 }
  redeem(coupon)
  expect(() => redeem(coupon)).toThrow('Coupon already used')
})

With the mutant, the second redeem(coupon) returns 5 instead of throwing.

Checks for the other statements:

  • A call: check what it changes, such as orders after orders.push(order). For save(order) from another module, mock the module and check toHaveBeenCalledWith(order).
  • A throw: call the function with input it must reject, and use toThrow.
  • A return: use input that reaches it, and check the result, such as null from checkout() for an empty cart.
  • The break of a case: use that case’s value and check the result, such as a discount of 10 for 'WELCOME'.
  • With jumps: check what a loop does after the continue or break, and every value that a generator yields.

Configuration

Turn the mutator off in the configuration:

import { defineConfig } from 'mootant'

export default defineConfig({
  mutators: {
    'statement-removal': 'off',
  },
})

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

// mootant-ignore-next-line statement-removal: only warms the image cache
preloadImages(product)

With strict, 'statement-removal': 'on' turns jumps back off: the options start from the defaults of the mutator, not from the preset.

jumps

false by default, true in strict. With jumps, Mootant also removes continue, yield and every break, not only the one that ends a case. Here, continue → ; makes the loop reserve an item that it has just put on back order:

for (let item of items) {
  if (!item.inStock) {
    backorder(item)
    continue
  }
  reserve(item)
}

Turn it on:

import { defineConfig } from 'mootant'

export default defineConfig({
  mutators: {
    'statement-removal': ['on', { jumps: true }],
  },
})

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
stock.set(sku, left - 1)→;Removes code that has a finer mutant insidesubsumedsolitary
applyCoupon(order)→; in if (order.coupon)Same result as a true or false of another mutator, for a test without an effectownedO19
addGift(order)→; in if (order.total > 100)Another mutant makes the if test false on some inputs, with the same effectssubsumedR4
console.log('Order saved')→;Text of a log message, such as in console.log()noiselog-message
return null→;Last return of no value, undefined or nullequivalenttail-return
break→; in the last caseFalls through only to the end or a breakequivalentfall-through-break
ship(queue.shift())→; in while (queue.length)May keep a loop running forevertimeoutloop-advance
return false→;Last return false: only false becomes undefinednoisetail-return-false
await setTimeout(500)→;Only waits for a timer delaynoisetimer-delay
continue→;continue that ends the loop body anywayequivalentloop-end
total = price * quantity→; in $:Mootant can’t switch a mutant on hereinvalidinvalid-target
defineProps(['price'])→;Read by the Vue or Svelte compiler as writteninvalidcompiler-macro

Changes that come back:

  • solitary, with the coarseMutants setting.
  • loop-advance, with the timeouts setting.
  • solitary and R4, when the other mutant doesn’t run, for example because its mutator is off.
  • log-message, when loggers leaves out console. In recommended, a call with text is then skipped as solitary.

strict turns on both settings. The other changes stay skipped.

A return of any other value, such as return createOrder(cart), belongs to return-value.