tryRun
Runs a function (synchronously), expecting it to throw an error.
tryRun(f: () => void): unknown
Summary
This method runs the provided function within a try-catch statement. If the function throws an error or if the test case fails otherwise, the error that was caught is returned and the test case doesn’t fail. If the function doesn’t throw an error, this method returns undefined.
Parameters
- f — The function to run
Return value
The error that was caught, or undefined otherwise
Errors
- This method throws the current error, if the test case already failed before (e.g. using fail())
Example
describe("My scope", () => {
test("Try to run a bad function", (t) => {
function bad() { let x = undefined; return x["foo"]; }
let error = t.tryRun(bad);
expect(error).toBeDefined();
});
});
Related
- class TestCaseA class that represents a single test case, part of a TestScope.
- tryRunAsync(f)Runs a function asynchronously, expecting it to throw an error.