15
Apr

Validating boolean return type of a function in Jest

Following code snippet demonstrates how to validate boolean return values from a custom function:
————————————————————————————————–

describe(“Functions to test return value of function in JavaScript”, function(){

function fetchData()
{
var response = false;
if(“peanut butter” === “peanut butter wax”)
{
response = true;
}
else{
response = false;
}
return response;
}

test(‘Expect true from return value of fetchData()’, ()=> {
expect(fetchData()).toBeTruthy();
});

})

Output:
——–

expect(received).toBeTruthy()

Expected value to be truthy, instead received
false

15 |
16 | test(‘Expect true from return value of fetchData()’, ()=> {
> 17 | expect(fetchData()).toBeTruthy();
18 | });
19 |
20 | })