|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1023 bytes
|
| Line | |
|---|
| 1 | # Require a message for `toThrow()` (`require-to-throw-message`)
|
|---|
| 2 |
|
|---|
| 3 | `toThrow()` (and its alias `toThrowError()`) is used to check if an error is
|
|---|
| 4 | thrown by a function call, such as in `expect(() => a()).toThrow()`. However, if
|
|---|
| 5 | no message is defined, then the test will pass for any thrown error. Requiring a
|
|---|
| 6 | message ensures that the intended error is thrown.
|
|---|
| 7 |
|
|---|
| 8 | ## Rule details
|
|---|
| 9 |
|
|---|
| 10 | This rule triggers a warning if `toThrow()` or `toThrowError()` is used without
|
|---|
| 11 | an error message.
|
|---|
| 12 |
|
|---|
| 13 | ### Default configuration
|
|---|
| 14 |
|
|---|
| 15 | The following patterns are considered warnings:
|
|---|
| 16 |
|
|---|
| 17 | ```js
|
|---|
| 18 | test('all the things', async () => {
|
|---|
| 19 | expect(() => a()).toThrow();
|
|---|
| 20 |
|
|---|
| 21 | expect(() => a()).toThrowError();
|
|---|
| 22 |
|
|---|
| 23 | await expect(a()).rejects.toThrow();
|
|---|
| 24 |
|
|---|
| 25 | await expect(a()).rejects.toThrowError();
|
|---|
| 26 | });
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | The following patterns are not considered warnings:
|
|---|
| 30 |
|
|---|
| 31 | ```js
|
|---|
| 32 | test('all the things', async () => {
|
|---|
| 33 | expect(() => a()).toThrow('a');
|
|---|
| 34 |
|
|---|
| 35 | expect(() => a()).toThrowError('a');
|
|---|
| 36 |
|
|---|
| 37 | await expect(a()).rejects.toThrow('a');
|
|---|
| 38 |
|
|---|
| 39 | await expect(a()).rejects.toThrowError('a');
|
|---|
| 40 | });
|
|---|
| 41 | ```
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.