Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

javascript - unable to handle async error in jest when error is thrown in if-else statement

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...