denoland / denoland/std

expect().toThrow(): support checking properties

Open
#6,805 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**

I can check that something will throw but I don't see a straightforward way to check properties on the thrown exception.

**Describe the solution you'd like**

```ts
expect(someFn).toThrow(new Error("kaboom", { cause: new TypeError("bad input") }));
```

or some way to pass in a function to `toThrow()` or to get the thrown exception to check things:

```ts
expect(someFn).toThrow(new Error("kaboom"), (e) => {
// more assertions, e.g. on `e.cause`
});
```

```ts
const thrownError = expect(someFn).toThrow(new Error("kaboom"));
// more assertions, e.g. on `thrownError.cause`
```

**Describe alternatives you've considered**

1. Calling the function direclty myself and wrapping with `try...catch` (I have to remember to add an explicit `fail()` if the function doesn't throw, otherwise the test may pass with a false positive).
2. Using some setup with a spy to catch the exception… not sure if that is a thing yet or not.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.