I have a file with two routes.
routes/index.js
const express = require('express')
const router = express.Router()
router.get('', (req, res, next) => {
try {
res.status(200).render('../views/')
} catch (error) {
next(error)
}
})
router.get('*', (req, res, next) => {
try {
res.status(404).render('../views/not-found')
} catch (error) {
next(error)
}
})
module.exports = router
I want to test the catch branches. One of the obvious problems is that I can't fake what the callback of router.get does, because then my test would be pointless, I would modify the very thing that I want to test.
I can send a Supertest request to these specific routes, but then I have no control over what happens. I want to avoid creating a hardcoded route, only so I can check whether the middleware is called by the next function. If I understand correctly, if I would have additional things happening in the try block (like a database query) than I could mock that to throw an error.
But I do not have any additional things happening there. Here is what I can not wrap my head around: if I mock what the server does, then I don't test what I already have but something else, so there is no point in doing that. Maybe I misunderstand how things work, but as far as I can see the only option would be to somehow mock what res.status or res.render does (stub them so they throw an error), so somehow mock what Supertest does, but I have no idea how to do that.
Any help would be greatly appreciated!