0
I have an application where I am trying to create a simple test to improve code coverage. I currently have a route that returns a health check, like this:
import { Express, Request, Response, Router } from 'express'
import { MongoHelper } from '../../infra/db/mongodb/helpers/mongo.helper'
import env from '../config/env'
export default (_router: Router, app: Express): void => {
app.use(env.app.healthCheckBasePath, (_req: Request, res: Response) => {
/*
O "MongoHelper" é uma função que contém a instância do MongoClient na propriedade "client"
Se verificarmos na documentação da própria biblioteca mongodb para node podemos ver que existe
o método "isConnected" que retorna se está ou não conectado no banco.
*/
if (!MongoHelper.client.isConnected()) {
/*
Aqui está a parte do código que preciso testar. Quando eu envio essa informação o Jest testa
apenas a parte de código ".status(500)" quando deveria testar o ".send" também
*/
return res.sendStatus(500)
}
return res.sendStatus(200)
})
}
My problem is that code coverage says I can’t test line 17 of this file (which is exactly the line that counts return res.status(500).send()
).
Does anyone have any idea how I can improve my code coverage and test the method .send
along those lines?
Usually when I want to test this situation I use the technique of making a request mock. I never worked with Jest, but the lib I use is the
nock
. Here’s an example of how to use my github, the quotation repository and tokenAuthDatastore.– Danizavtz
But I’m working with
jest
and also need to test the methodsendStatus
...– LeandroLuk
It is not necessary to "change" your tool, the
nock
has to be used together to assist you in this test, you do not need to stop using thejest
.– Danizavtz