|
| 1 | +# prefer toBeRequired() or not.toBeRequired() over toHaveProperty('required', true|false) (prefer-enabled-required) |
| 2 | + |
| 3 | +## Rule Details |
| 4 | + |
| 5 | +This rule aims to prevent false positives and improve readability and should only be used with the `@testing-library/jest-dom` package. See below for examples of those potential issues and why this rule is recommended. The rule is autofixable and will replace any instances of `.toHaveProperty()` or `.toHaveAttribute()` with `toBeRequired()` or `not.toBeRequired` as appropriate. |
| 6 | + |
| 7 | +### False positives |
| 8 | + |
| 9 | +Consider these 2 snippets: |
| 10 | + |
| 11 | +```js |
| 12 | +const { getByRole } = render(<input type="checkbox" required />); |
| 13 | +const element = getByRole('checkbox'); |
| 14 | +expect(element).toHaveProperty('required'); // passes |
| 15 | + |
| 16 | +const { getByRole } = render(<input type="checkbox" />); |
| 17 | +const element = getByRole('checkbox'); |
| 18 | +expect(element).toHaveProperty('required'); // also passes 😱 |
| 19 | +``` |
| 20 | + |
| 21 | +### Readability |
| 22 | + |
| 23 | +Consider the following snippets: |
| 24 | + |
| 25 | +```js |
| 26 | +const { getByRole } = render(<input type="checkbox" />); |
| 27 | +const element = getByRole('checkbox'); |
| 28 | + |
| 29 | +expect(element).toHaveAttribute('required', false); // fails |
| 30 | +expect(element).toHaveAttribute('required', ''); // fails |
| 31 | +expect(element).not.toHaveAttribute('required', ''); // passes |
| 32 | + |
| 33 | +expect(element).not.toHaveAttribute('required', true); // passes. |
| 34 | +expect(element).not.toHaveAttribute('required', false); // also passes. |
| 35 | +``` |
| 36 | + |
| 37 | +As you can see, using `toHaveAttribute` in this case is confusing, unintuitive and can even lead to false positive tests. |
| 38 | + |
| 39 | +Examples of **incorrect** code for this rule: |
| 40 | + |
| 41 | +```js |
| 42 | +expect(element).toHaveProperty('required', true); |
| 43 | +expect(element).toHaveAttribute('required', false); |
| 44 | + |
| 45 | +expect(element).toHaveAttribute('required'); |
| 46 | +expect(element).not.toHaveProperty('required'); |
| 47 | + |
| 48 | +expect(element).not.toBeRequired(); |
| 49 | +expect(element).not.not.toBeRequired(); |
| 50 | +``` |
| 51 | + |
| 52 | +Examples of **correct** code for this rule: |
| 53 | + |
| 54 | +```js |
| 55 | +expect(element).not.toBeRequired(); |
| 56 | + |
| 57 | +expect(element).toBeRequired(); |
| 58 | + |
| 59 | +expect(element).toHaveProperty('aria-label', 'foo'); |
| 60 | + |
| 61 | +expect(element).toHaveAttribute('alt'); |
| 62 | +``` |
| 63 | + |
| 64 | +## When Not To Use It |
| 65 | + |
| 66 | +Don't use this rule if you: |
| 67 | + |
| 68 | +- don't use `jest-dom` |
| 69 | +- want to allow `.toHaveProperty('required', true|false);` |
| 70 | + |
| 71 | +## Further reading |
| 72 | + |
| 73 | +- [toBeRequired](https://github.com/testing-library/jest-dom#toBeRequired) |
| 74 | +- [not.toBeRequired](https://github.com/testing-library/jest-dom#not.toBeRequired) |
0 commit comments