EasyJavaScriptTypeScript

Implement .toThrow() Matcher

TestingJavaScriptErrors

Implement solve(serializedFn, expectedMsg?) where serializedFn is the
stringified source of a zero-argument function — and your code must
reconstruct and call it to observe whether it throws.

solve must handle these cases:

CallBehaviour
solve(fn, undefined)Passes if the function throws anything.
solve(fn, 'msg')Passes if the error message contains 'msg'.
solve(fn, null)Passes if the function does not throw.

Return { pass: boolean, message: string }.

// throws anything
solve('function(){throw new Error("boom")}')
// → { pass: true, message: '' }

// throws with specific message
solve('function(){throw new Error("boom")}', 'boom')
// → { pass: true, message: '' }

// message mismatch
solve('function(){throw new Error("boom")}', 'oops')
// → { pass: false, message: "Expected error message to contain 'oops' but got 'boom'" }

// should not throw
solve('function(){return 42}', null)
// → { pass: true, message: '' }

Sample tests

Test #1throws any error — passes
Input: ["function(){throw new Error(\"boom\")}",null]
Output: {"pass":true,"message":""}
Test #2does not throw — fails
Input: ["function(){return 42}",null]
Output: {"pass":false,"message":"Expected function to throw but it did not"}
Test #3message contains expected string — passes
Input: ["function(){throw new Error(\"boom\")}","boom"]
Output: {"pass":true,"message":""}
Test #4message mismatch — fails
Input: ["function(){throw new Error(\"boom\")}","oops"]
Output: {"pass":false,"message":"Expected error message to contain 'oops' but got 'boom'"}
Test #5does not throw — null expects no throw — passes
Input: ["function(){return 42}",null]
Output: {"pass":true,"message":""}