2020-05-15 05:33:08 +08:00
|
|
|
# Require a message for `toThrow()` (`require-to-throw-message`)
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
`toThrow()` (and its alias `toThrowError()`) is used to check if an error is
|
2020-03-07 11:45:40 +08:00
|
|
|
thrown by a function call, such as in `expect(() => a()).toThrow()`. However, if
|
|
|
|
no message is defined, then the test will pass for any thrown error. Requiring a
|
|
|
|
message ensures that the intended error is thrown.
|
|
|
|
|
|
|
|
## Rule details
|
|
|
|
|
|
|
|
This rule triggers a warning if `toThrow()` or `toThrowError()` is used without
|
|
|
|
an error message.
|
|
|
|
|
|
|
|
### Default configuration
|
|
|
|
|
|
|
|
The following patterns are considered warnings:
|
|
|
|
|
|
|
|
```js
|
2020-09-13 06:19:45 +08:00
|
|
|
test('all the things', async () => {
|
|
|
|
expect(() => a()).toThrow();
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
expect(() => a()).toThrowError();
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
await expect(a()).rejects.toThrow();
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
await expect(a()).rejects.toThrowError();
|
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
The following patterns are not considered warnings:
|
|
|
|
|
|
|
|
```js
|
2020-09-13 06:19:45 +08:00
|
|
|
test('all the things', async () => {
|
|
|
|
expect(() => a()).toThrow('a');
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
expect(() => a()).toThrowError('a');
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
await expect(a()).rejects.toThrow('a');
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
await expect(a()).rejects.toThrowError('a');
|
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
```
|