How do I assert that thou dost throw? Let me count the ways.
Synchronous method
const act: () => unknown = () => {
throw new Error('wow');
};
expect(act).toThrowError('wow');
A promise that rejects with a string
const promise1: Promise<unknown> = new Promise((resolve, reject) => reject('bad news'));
await expectAsync(promise1).toBeRejectedWith('bad news');
A promise that rejects with an error
const promise2: Promise<unknown> = new Promise((resolve, reject) =>
reject(new Error('more bad news')));
await expectAsync(promise2).toBeRejectedWithError('more bad news');
An async method that throws (i.e. a promise that rejects with an error)
async function someAsyncFunction() {
throw new Error('very bad news');
}
const promise3: Promise<unknown> = someAsyncFunction();
await expectAsync(promise3).toBeRejectedWithError('very bad news');