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.
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 |
|---|---|---|
|
'' |
String |
|
0 |
Number |
|
0n |
BigInt |
|
[] |
Array |
|
true and false |
Boolean |
|
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.
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)undefinedin an arrow function that returnsvoid, so the product is never added.→this.loadingundefinedinasync whenReady(): Promise<void>, so the function no longer waits forthis.loading.
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 |
|---|---|---|
|
Promise.resolve(0) |
Promise<number> |
|
Promise.resolve(null) |
Promise<Product | null> |
|
Promise.resolve(void 0) |
Promise<Cart> |
void 0 is undefined.
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 → sum0 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 → 01 in let sum = 0, but not → sum0. The
coarseMutants setting brings these
mutants back.
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.length0, 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,[]andundefined: an input with another result, andtoBeortoEqual.toBeDefined()catches onlyundefined.trueandfalse: one test for each result. For the callback ofsome(), test a list with a product in stock and one whose products are all sold out.Promise.resolve(…):awaitthe result and check its value.undefinedin avoidfunction: check what the removed call does, such as the product thataddToCartadds.undefinedfor a promise in anasyncfunction: check that the function rejects when that promise rejects.
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.
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 |
|---|---|
price * quantity→0 | Removes code that has a finer mutant insidesubsumedsolitary |
!product.stock→false | Same result as a true or false of another mutatorownedO19 |
cart.total >= 50→true | Another mutant gives this value on some inputssubsumedR6 |
typeof sku === 'string'→true | Type hints show the comparison already has this resultequivalentproven-comparison |
cart.items.length >= 0→true | Same 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)→undefined | Returned logging call, such as console.log()noiselogging-call |
cart.total→undefined | Value without an effect in a void functionequivalentvoid-result |
cart.express ?? false→false | Same code as another mutantduplicateduplicate |
Changes that come back:
solitary, with thecoarseMutantssetting, on instrict.solitaryandR6, when the other mutant doesn’t run, for example because its mutator is off.R6for a returned variable, when other code also uses it, such asif (free)beforereturn free. A statementfree = …that only assigns it is not a use.logging-call, whenloggersleaves out the logger.
The other changes stay skipped.
A returned literal, such as 'free', belongs to its literal mutator, here
string-literal.