I’m writing a suite of Jest tests, and the following question came up. How can I test that a function simply returns if it receives an invalid argument? For example, I would like to test that the function changeAge
below returns immediately if it receives a newAge
or id
that is not a whole number.
function changeAge (id, newAge) {
const isWholeNumber = num => Number.isInteger(newAge) && newAge >= 0
if (!isWholeNumber(id) || !isWholeNumber(newAge)) {
return
}
const person = getPerson(id)
person.age = newAge
}
In this example, there’s no way in the test to retrieve the person data to verify whether the age property was changed.