You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1
Original file line number
Diff line number
Diff line change
@@ -143,6 +143,7 @@ To enable this configuration use the `extends` property in your
143
143
|[prefer-explicit-assert](docs/rules/prefer-explicit-assert.md)| Suggest using explicit assertions rather than just `getBy*` queries |||
144
144
|[prefer-find-by](docs/rules/prefer-find-by.md)| Suggest using `findBy*` methods instead of the `waitFor` + `getBy` queries |![dom-badge][]![angular-badge][]![react-badge][]![vue-badge][]|![fixable-badge][]|
145
145
|[prefer-presence-queries](docs/rules/prefer-presence-queries.md)| Enforce specific queries when checking element is present or not |||
146
+
|[prefer-user-event](docs/rules/prefer-user-event.md)| Suggest using `userEvent` library instead of `fireEvent` for simulating user interaction |![dom-badge][]![angular-badge][]![react-badge][]![vue-badge][]||
146
147
|[prefer-screen-queries](docs/rules/prefer-screen-queries.md)| Suggest using screen while using queries |![dom-badge][]![angular-badge][]![react-badge][]![vue-badge][]||
147
148
|[prefer-wait-for](docs/rules/prefer-wait-for.md)| Use `waitFor` instead of deprecated wait methods ||![fixable-badge][]|
> [...] it is becoming apparent the need to express user actions on a web page
7
+
> using a higher-level abstraction than `fireEvent`
8
+
9
+
`userEvent` adds related event calls from browsers to make tests more realistic than its counterpart `fireEvent`, which is a low-level api.
10
+
See the appendix at the end to check how are the events from `fireEvent` mapped to `userEvent`.
11
+
12
+
## Rule Details
13
+
14
+
This rule enforces the usage of [userEvent](https://github.com/testing-library/user-event) methods over `fireEvent`. By default, the methods from `userEvent` take precedence, but you add exceptions by configuring the rule in `.eslintrc`.
15
+
16
+
Examples of **incorrect** code for this rule:
17
+
18
+
```ts
19
+
// a method in fireEvent that has a userEvent equivalent
// using fireEvent after importing the entire library
28
+
import*asdomfrom'@testing-library/dom';
29
+
dom.fireEvent.click(node);
30
+
```
31
+
32
+
Examples of **correct** code for this rule:
33
+
34
+
```ts
35
+
importuserEventfrom'@testing-library/user-event';
36
+
37
+
// any userEvent method
38
+
userEvent.click();
39
+
40
+
// fireEvent method that does not have an alternative in userEvent
41
+
fireEvent.cut(node);
42
+
43
+
import*asdomfrom'@testing-library/dom';
44
+
dom.fireEvent.cut(node);
45
+
```
46
+
47
+
#### Options
48
+
49
+
This rule allows to exclude specific functions with an equivalent in `userEvent` through configuration. This is useful if you need to allow an event from `fireEvent` to be used in the solution. For specific scenarios, you might want to consider disabling the rule inline.
50
+
51
+
The configuration consists of an array of strings with the names of fireEvents methods to be excluded.
52
+
An example looks like this
53
+
54
+
```json
55
+
{
56
+
"rules": {
57
+
"prefer-user-event": [
58
+
"error",
59
+
{
60
+
"allowedMethods": ["click", "change"]
61
+
}
62
+
]
63
+
}
64
+
}
65
+
```
66
+
67
+
With this configuration example, the following use cases are considered valid
When you don't want to use `userEvent`, such as if a legacy codebase is still using `fireEvent` or you need to have more low-level control over firing events (rather than the recommended approach of testing from a user's perspective)
-[userEvent in the react-testing-library docs](https://testing-library.com/docs/ecosystem-user-event)
94
+
95
+
## Appendix
96
+
97
+
The following table lists all the possible equivalents from the low-level API `fireEvent` to the higher abstraction API `userEvent`. All the events not listed here do not have an equivalent (yet)
98
+
99
+
| fireEvent method | Possible options in userEvent |
0 commit comments