boolean-literal
let freeShipping = false let freeShipping = true This mutator flips a boolean literal, as if a flag or a default were set the
wrong way: let freeShipping = false becomes let freeShipping = true.
Tests
catch it with a case where the literal decides the result, such as a call of
shippingCost() that relies on the default express = false.
Each true becomes false, and each false becomes true:
| Code | Mutant |
|---|---|
let freeShipping = |
let freeShipping = true |
let options = { giftWrap: |
let options = { giftWrap: false } |
applyCoupon(cart, |
applyCoupon(cart, false) |
function quote(express = |
function quote(express = true) { |
while ( |
while (false) |
Mootant leaves alone a literal in a comparison, such as true in
inStock === true. It also leaves alone the two results of one condition
without an effect, as in inStock ? true : false or in
if (inStock) { return true } before return false; see
Skipped changes.
Write a test in which the literal decides the result. Take this function:
export function shippingCost(express = false): number {
export function shippingCost(express = true): number {
return express ? 15 : 5
}The mutant express = → falseexpress = true survives tests that always pass
express, such as shippingCost(true) and shippingCost(false): they never
use the default. Call the function without the argument:
it('uses standard shipping by default', () => {
expect(shippingCost()).toBe(5)
})With the mutant, shippingCost() returns 15 instead of 5.
Literals in other places need other tests:
let soldOut = falsethat a loop may set: test a cart with nothing sold out.while (true): check a result of the loop, whichwhile (false)never runs.trueinapplyCoupon(cart, true): check a result thatfalsewould change.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'boolean-literal': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line boolean-literal: debug output only
let verbose = falseThis 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 |
|---|---|
true→false in inStock ? true : false | Same result as a true or false of another mutator, for a condition without an effectownedO19 |
true→false in inStock === true | Literal in a comparison, left to comparison-operatorsubsumedO4 |
true→false in price < 10 ? true : false | Left to a finer mutant of the condition, with the same effectssubsumedR5 |
true→false in paid = true inside while (!paid) | May keep a loop running forevertimeoutloop-advance |
false→true in if (vip || false) | Side of && or ||, left to logical-operatorownedO3 |
false→true in do { send() } while (false) | Makes a loop condition always truetimeoutloop-running |
true→false in let send = receipt || true with receipt?: boolean | logical-operator swaps || for ?? insteadsubsumedR7 |
true→false in a sort comparator | method-call removes the sort() call insteadownedO12 |
true→false in void true | Operand of void, whose value is always undefinedequivalentvoid-operand |
The timeouts setting, on in strict, brings
back loop-advance and loop-running. O4, R5 and R7 come back when the
finer mutant doesn’t run: its mutator is off or ignored on that line. The other
changes stay skipped.