Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...