I used all the techniques to test a route that throws an error in the if statement but I'm unable to pass the test.
router.post('/api/users/signUp', async (req: Request, res: Response) => {
const { email, password } = req.body;
const error = validateAuthInput({ email, password });
if (error) {
console.log(error) // "email" must be longer than 5 characters or so
throw new Error(error);
}
})
I think throwing an error with an if-statement makes my tests fail. I tried every technique to expect an error but unable to test.
it('throws an Error', async () => {
const res = request(app)
.post('/api/users/signUp')
.send({ email: 'test', password: 'password' });
await expect(res).rejects.toThrow();
});
The request is handled by supertest library.
I tried all the ways to expect the result but failed to do so.
question from:
https://stackoverflow.com/questions/65901761/unable-to-handle-async-error-in-jest-when-error-is-thrown-in-if-else-statement 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…