Is it possible to test that a code path is not executed in a test case in Jest
? For example, in the following test, I want the then
part to execute (which has an expect
but I don't want the catch
part to execute. Will jest
automatically fail the test if catch
part is hit and it can't find an expect
in it?
describe("Endpoints are accessible", () => {
it("GET request for homepage should return 200", async () => {
const baseURL = process.env.EXPRESS_SERVER_TEST
console.log(`base url is ${baseURL}`);
const req = {};
const res = {};
indexControllerModule.homepageHandler(req,res)
.then((result)=>{
console.log(`result is ${result}`);
expect(result).toBe(JSON.stringify({
"message": "welcome"
}));
})
.catch((error)=>{
console.log(`This code path shouldn't be executed. Error in accessing homepage: ${error}`);
})
});
});