codejump
EasyPro challengePythonJavaScriptTypeScript

Implement .toThrow() Matcher

TestingErrors

Jest's expect(fn).toThrow() does not receive a value — it receives a function, calls it, and checks whether it throws.

The starter code defines a few functions under test in SAMPLES. Implement the two matchers:

MatcherPasses when
toThrow(fn)fn() throws anything
toThrow(fn, expected)fn() throws and the error message contains expected
notToThrow(fn)fn() does not throw

solve(name, matcher, expected?) looks up SAMPLES[name] and runs 'toThrow' or 'not.toThrow' on it.

Return { pass, message } — message is '' when the matcher passes, otherwise one of:

  • "Expected function to throw but it did not"
  • "Expected error message to contain 'oops' but got 'boom'"
  • "Expected function not to throw but it threw: oops"
solve('throwsBoom', 'toThrow')          // { pass: true,  message: '' }
solve('throwsBoom', 'toThrow', 'oops')  // { pass: false, message: "Expected error message to contain 'oops' but got 'boom'" }
solve('returns42', 'not.toThrow')       // { pass: true,  message: '' }

Sample tests

Test #1toThrow — any error passes
Input: ["throwsBoom","toThrow"]
Output: {"pass":true,"message":""}
Test #2toThrow — no throw fails
Input: ["returns42","toThrow"]
Output: {"pass":false,"message":"Expected function to throw but it did not"}
Test #3toThrow — message matches
Input: ["throwsBoom","toThrow","boom"]
Output: {"pass":true,"message":""}
Test #4toThrow — message mismatch fails
Input: ["throwsBoom","toThrow","oops"]
Output: {"pass":false,"message":"Expected error message to contain 'oops' but got 'boom'"}
Test #5not.toThrow — no throw passes
Input: ["returns42","not.toThrow"]
Output: {"pass":true,"message":""}