I' testing endpoint
router.post("/changeRadius", async (req, res) => {
try {
const radius = req.body.radius;
const id = req.body.id;
if (radius === null)
return res.status(400).send({message: "Nie podano nowego promienia"})
const updateRadius = {
radius: radius
};
await models.user.update(updateRadius, {
where: {
id: id
}
})
res.status(201).send({ message: "Promień został pomyślnie zmieniony" })
} catch (error) {
console.log(error)
res.status(500).send({ message: "Nie podano nowego promienia" })
}
})
The test looks like this
it('should handle server errors', async () => {
jest.spyOn(models.user, 'findOne').mockImplementation(() => {
throw new Error('Błąd bazy danych');
});
const res = await request(app).post('/api/users/changeAlias').send({ alias: 'someAlias', id: 1 });
expect(res.statusCode).toEqual(500);
expect(res.body).toHaveProperty('message', 'Błąd serwera');
});
It's working when i normally close the connection with db (sequelize.close()), but i want to use spyOn, it gives the 201 status instead of 500 so the spyOn function doesn't even execute.