Testing · Junior · Question 19 of 78
How do you check that a function throws an error?
expect(() => parse("bad")).toThrow("Invalid input");- ✓Correct answer: Pass a function that calls the code, so
expectcan catch the error - 2Call
parse("bad")directly insideexpect - 3Use
toBeFalsy()on the result
Explanation
expect(parse("bad")) would throw before expect gets control and the test would fail with that error instead of checking it.