Testing · Junior · Question 19 of 78

How do you check that a function throws an error?

expect(() => parse("bad")).toThrow("Invalid input");

Explanation

expect(parse("bad")) would throw before expect gets control and the test would fail with that error instead of checking it.

Further reading

Problem with a question

Testing · Junior · question 19

Test yourself

Take the Testing · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz