On this page

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.

Changes

Each true becomes false, and each false becomes true:

Code Mutant
let freeShipping = false let freeShipping = true
let options = { giftWrap: true } let options = { giftWrap: false }
applyCoupon(cart, true) applyCoupon(cart, false)
function quote(express = false) { function quote(express = true) {
while (true) 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.

Kill a surviving mutant

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 = false → express = 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 = false that a loop may set: test a cart with nothing sold out.
  • while (true): check a result of the loop, which while (false) never runs.
  • true in applyCoupon(cart, true): check a result that false would change.

Configuration

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 = false

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
true→false in inStock ? true : falseSame result as a true or false of another mutator, for a condition without an effectownedO19
true→false in inStock === trueLiteral in a comparison, left to comparison-operatorsubsumedO4
true→false in price < 10 ? true : falseLeft 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?: booleanlogical-operator swaps || for ?? insteadsubsumedR7
true→false in a sort comparatormethod-call removes the sort() call insteadownedO12
true→false in void trueOperand 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.