# return-value

```diff
- 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](#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`](https://mootant.org/guide/configuration.md#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:

```ts
interface Cart {
  items: string[]
}

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

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:

```ts
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](https://mootant.org/guide/configuration.md#mutators):

```ts
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:

```ts
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](https://mootant.org/mutators.md#skipped-changes)
explains the categories.

| Reason                                                                 | Example                                 | Rule                            |
| ---------------------------------------------------------------------- | --------------------------------------- | ------------------------------- |
| Removes code that has a finer mutant inside                            | `price * quantity` → `0`                | `solitary`, subsumed            |
| Same result as a `true` or `false` of another mutator                  | `!product.stock` → `false`              | `O19`, owned                    |
| Another mutant gives this value on some inputs                         | `cart.total >= 50` → `true`             | `R6`, subsumed                  |
| Type hints show the comparison already has this result                 | `typeof sku === 'string'` → `true`      | `proven-comparison`, equivalent |
| Same result, assuming a lower limit, such as 0 for a `length`          | `cart.items.length >= 0` → `true`       | `lower-bound`, equivalent       |
| [method-call](https://mootant.org/mutators/method-call.md) removes the `sort()` call instead | `a.price - b.price` → `0` in `sort()`   | `O12`, owned                    |
| Returned logging call, such as `console.log()`                         | `console.log(cart.total)` → `undefined` | `logging-call`, noise           |
| Value without an effect in a `void` function                           | `cart.total` → `undefined`              | `void-result`, equivalent       |
| Same code as another mutant                                            | `cart.express ?? false` → `false`       | `duplicate`, duplicate          |

Changes that come back:

- `solitary`, with the [`coarseMutants`](https://mootant.org/guide/configuration.md#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`](https://mootant.org/guide/configuration.md#loggers) leaves out the
  logger.

The other changes stay skipped.

A returned literal, such as `'free'`, belongs to its literal mutator, here
[string-literal](https://mootant.org/mutators/string-literal.md).
