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.
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.
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).
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.
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
ifwithoutelse, such as thethrowinif (!coupon) throw new Error('No coupon'): another mutant already makes the testfalse. The removal stays when that mutant would skip an effect of the test, such as the call inif (reserve(sku)). Forif (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 ascount++,await save(order)orreturn 0, unless it is a plain=assignment. Mootant assumes that a test that catches the finer mutant also catches the removal. - A
consolecall whose arguments are only text, such asconsole.log('Order saved'). Mootant removes other log calls, such asconsole.log('Saved', order.id)andlogger.info('Saved').
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
ordersafterorders.push(order). Forsave(order)from another module, mock the module and checktoHaveBeenCalledWith(order). - A
throw: call the function with input it must reject, and usetoThrow. - A
return: use input that reaches it, and check the result, such asnullfromcheckout()for an empty cart. - The
breakof 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 thecontinueorbreak, and every value that a generator yields.
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.
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 }],
},
})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 |
|---|---|
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 case | Falls 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 thecoarseMutantssetting.loop-advance, with thetimeoutssetting.solitaryandR4, when the other mutant doesn’t run, for example because its mutator is off.log-message, whenloggersleaves outconsole. Inrecommended, a call with text is then skipped assolitary.
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.