On this page

return-value

cart.items.length 0

This mutator makes a function return a fixed value, such as 0, '', [] or undefined, in place of its result; type hints decide which. return cart.items.length becomes return 0, as if the function lost the count it computed.

Tests catch it with an input for which the function returns something else, such as a cart with items, and a check of the exact value.

Changes

Return a fixed value

In a return statement and in an arrow function without braces, the returned value becomes a fixed value that Mootant picks from type hints:

Code Mutant Type
product.name '' String
cart.items.length 0 Number
order.cents 0n BigInt
cart.items [] Array
hasCoupon(cart) true and false Boolean
lookup(sku) undefined Any other, such as an object

Mootant reads the type from an annotation or from the return type of the function. When neither is known, the mutant is undefined: for product.name in a JavaScript file, or in products.map(product => product.name), where product has no annotation. An async function gets the plain value, such as 0, not a promise.

The result of a callback of filter, some, every or a find method counts as a boolean: product.stock becomes true and false in products.some(product => product.stock). A returned comparison, !, && or || usually gets neither: Mootant skips one or both when a test that catches another mutant also catches this change. Skipped changes says which come back.

Remove what a void function does

In a function that returns void, or Promise<void> when it is async, the mutant undefined removes what the returned expression does:

  • addToCart(cart, product) → undefined in an arrow function that returns void, so the product is never added.
  • this.loading → undefined in async whenReady(): Promise<void>, so the function no longer waits for this.loading.

Return a resolved promise

A function that returns Promise<T> without async gets a promise resolved with a fixed value that Mootant picks from T:

Code Mutant Return type
fetchStock(sku) Promise.resolve(0) Promise<number>
fetchProduct(id) Promise.resolve(null) Promise<Product | null>
loadCart(id) Promise.resolve(void 0) Promise<Cart>

void 0 is undefined.

Skip a value with a finer mutant inside

In recommended, this mutator skips returned values with a finer mutant inside, such as price * quantity. Boolean values are an exception: they can still get return-value mutants.

This rule also applies to a returned local variable or one of its properties when its initial value or a later assignment has a finer mutant. For example, after let sum = 0, the sum → 0 mutant is skipped because the initial 0 has its own mutant.

Other uses of the variable can bring the return-value mutant back:

Use of the variable Brings the mutant back?
Only an assignment, such as sum += price No
push() or unshift() on an array initialized as a literal No
Any other use, such as notify(sum) Yes

This skip rests on a guess: a test that catches price / quantity also catches 0. The guess can fail: for a function that sums a list, a test with an empty list catches 0 → 1 in let sum = 0, but not sum → 0. The coarseMutants setting brings these mutants back.

Kill a surviving mutant

Check the exact value that the function returns, with an input for which it returns something other than the mutant’s value. Take this function:

interface Cart {
  items: string[]
}

export function itemCount(cart: Cart): number {
  return cart.items.length
  return 0
}

A test with an empty cart does not catch cart.items.length → 0, because the function returns 0 for an empty cart too. Test it with items in the cart:

it('counts the items in the cart', () => {
  expect(itemCount({ items: ['book', 'pen'] })).toBe(2)
})

With the mutant, itemCount returns 0 instead of 2.

Checks for the other mutants:

  • '', 0n, [] and undefined: an input with another result, and toBe or toEqual. toBeDefined() catches only undefined.
  • true and false: one test for each result. For the callback of some(), test a list with a product in stock and one whose products are all sold out.
  • Promise.resolve(…): await the result and check its value.
  • undefined in a void function: check what the removed call does, such as the product that addToCart adds.
  • undefined for a promise in an async function: check that the function rejects when that promise rejects.

Configuration

Turn the mutator off in the configuration:

import { defineConfig } from 'mootant'

export default defineConfig({
  mutators: {
    'return-value': 'off',
  },
})

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

export function requestId(): string {
  // mootant-ignore-next-line return-value: trace id, only logged
  return crypto.randomUUID()
}

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
price * quantity→0Removes code that has a finer mutant insidesubsumedsolitary
!product.stock→falseSame result as a true or false of another mutatorownedO19
cart.total >= 50→trueAnother mutant gives this value on some inputssubsumedR6
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
a.price - b.price→0 in sort()method-call removes the sort() call insteadownedO12
console.log(cart.total)→undefinedReturned logging call, such as console.log()noiselogging-call
cart.total→undefinedValue without an effect in a void functionequivalentvoid-result
cart.express ?? false→falseSame code as another mutantduplicateduplicate

Changes that come back:

  • solitary, with the coarseMutants setting, on in strict.
  • solitary and R6, when the other mutant doesn’t run, for example because its mutator is off.
  • R6 for a returned variable, when other code also uses it, such as if (free) before return free. A statement free = … that only assigns it is not a use.
  • logging-call, when loggers leaves out the logger.

The other changes stay skipped.

A returned literal, such as 'free', belongs to its literal mutator, here string-literal.