Docs (4.0.0)
class TestCase method

tryRunAsync

Runs a function asynchronously, expecting it to throw an error.

tryRunAsync(f: () => Promise<void>): Promise<unknown>

Summary

This method calls the provided function asynchronously within a try-catch statement. If the function throws an error (or returns a promise that’s rejected) or if the test case fails otherwise, the error that was caught is returned and the test case won’t fail. If the function doesn’t throw an error, this method returns undefined.

Note
This method is asynchronous and must be await-ed.

Parameters

  • f — The function to run

Return value

A promise for 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 async function", async (t) => {
    async function bad() { let x = undefined; return x["foo"]; }
    let error = await t.tryRunAsync(bad);
    expect(error).toBeDefined();
  });
});

Related