|  | 
|  | 1 | +# Enforce promises from async event methods are handled (`testing-library/await-async-events`) | 
|  | 2 | + | 
|  | 3 | +💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`. | 
|  | 4 | + | 
|  | 5 | +🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | 
|  | 6 | + | 
|  | 7 | +<!-- end auto-generated rule header --> | 
|  | 8 | + | 
|  | 9 | +Ensure that promises returned by `userEvent` (v14+) async methods or `fireEvent` (only Vue and Marko) async methods are handled properly. | 
|  | 10 | + | 
|  | 11 | +## Rule Details | 
|  | 12 | + | 
|  | 13 | +This rule aims to prevent users from forgetting to handle promise returned from async event | 
|  | 14 | +methods. | 
|  | 15 | + | 
|  | 16 | +> ⚠️ `fireEvent` methods are async only on following Testing Library packages: | 
|  | 17 | +> | 
|  | 18 | +> - `@testing-library/vue` (supported by this plugin) | 
|  | 19 | +> - `@testing-library/svelte` (not supported yet by this plugin) | 
|  | 20 | +> - `@marko/testing-library` (supported by this plugin) | 
|  | 21 | +
 | 
|  | 22 | +Examples of **incorrect** code for this rule: | 
|  | 23 | + | 
|  | 24 | +```js | 
|  | 25 | +fireEvent.click(getByText('Click me')); | 
|  | 26 | + | 
|  | 27 | +fireEvent.focus(getByLabelText('username')); | 
|  | 28 | +fireEvent.blur(getByLabelText('username')); | 
|  | 29 | + | 
|  | 30 | +// wrap a fireEvent method within a function... | 
|  | 31 | +function triggerEvent() { | 
|  | 32 | +	return fireEvent.click(button); | 
|  | 33 | +} | 
|  | 34 | +triggerEvent(); // ...but not handling promise from it is incorrect too | 
|  | 35 | +``` | 
|  | 36 | + | 
|  | 37 | +```js | 
|  | 38 | +userEvent.click(getByText('Click me')); | 
|  | 39 | +userEvent.tripleClick(getByText('Click me')); | 
|  | 40 | +userEvent.keyboard('foo'); | 
|  | 41 | + | 
|  | 42 | +// wrap a userEvent method within a function... | 
|  | 43 | +function triggerEvent() { | 
|  | 44 | +	return userEvent.click(button); | 
|  | 45 | +} | 
|  | 46 | +triggerEvent(); // ...but not handling promise from it is incorrect too | 
|  | 47 | +``` | 
|  | 48 | + | 
|  | 49 | +Examples of **correct** code for this rule: | 
|  | 50 | + | 
|  | 51 | +```js | 
|  | 52 | +// `await` operator is correct | 
|  | 53 | +await fireEvent.focus(getByLabelText('username')); | 
|  | 54 | +await fireEvent.blur(getByLabelText('username')); | 
|  | 55 | + | 
|  | 56 | +// `then` method is correct | 
|  | 57 | +fireEvent.click(getByText('Click me')).then(() => { | 
|  | 58 | +	// ... | 
|  | 59 | +}); | 
|  | 60 | + | 
|  | 61 | +// return the promise within a function is correct too! | 
|  | 62 | +const clickMeArrowFn = () => fireEvent.click(getByText('Click me')); | 
|  | 63 | + | 
|  | 64 | +// wrap a fireEvent method within a function... | 
|  | 65 | +function triggerEvent() { | 
|  | 66 | +	return fireEvent.click(button); | 
|  | 67 | +} | 
|  | 68 | +await triggerEvent(); // ...and handling promise from it is correct also | 
|  | 69 | + | 
|  | 70 | +// using `Promise.all` or `Promise.allSettled` with an array of promises is valid | 
|  | 71 | +await Promise.all([ | 
|  | 72 | +	fireEvent.focus(getByLabelText('username')), | 
|  | 73 | +	fireEvent.blur(getByLabelText('username')), | 
|  | 74 | +]); | 
|  | 75 | +``` | 
|  | 76 | + | 
|  | 77 | +```js | 
|  | 78 | +// `await` operator is correct | 
|  | 79 | +await userEvent.click(getByText('Click me')); | 
|  | 80 | +await userEvent.tripleClick(getByText('Click me')); | 
|  | 81 | + | 
|  | 82 | +// `then` method is correct | 
|  | 83 | +userEvent.keyboard('foo').then(() => { | 
|  | 84 | +	// ... | 
|  | 85 | +}); | 
|  | 86 | + | 
|  | 87 | +// return the promise within a function is correct too! | 
|  | 88 | +const clickMeArrowFn = () => userEvent.click(getByText('Click me')); | 
|  | 89 | + | 
|  | 90 | +// wrap a userEvent method within a function... | 
|  | 91 | +function triggerEvent() { | 
|  | 92 | +	return userEvent.click(button); | 
|  | 93 | +} | 
|  | 94 | +await triggerEvent(); // ...and handling promise from it is correct also | 
|  | 95 | + | 
|  | 96 | +// using `Promise.all` or `Promise.allSettled` with an array of promises is valid | 
|  | 97 | +await Promise.all([ | 
|  | 98 | +	userEvent.click(getByText('Click me')); | 
|  | 99 | +	userEvent.tripleClick(getByText('Click me')); | 
|  | 100 | +]); | 
|  | 101 | +``` | 
|  | 102 | + | 
|  | 103 | +## Options | 
|  | 104 | + | 
|  | 105 | +- `eventModule`: `string` or `string[]`. Which event module should be linted for async event methods. Defaults to `userEvent` which should be used after v14. `fireEvent` should only be used with frameworks that have async fire event methods. | 
|  | 106 | + | 
|  | 107 | +## Example | 
|  | 108 | + | 
|  | 109 | +```json | 
|  | 110 | +{ | 
|  | 111 | +	"testing-library/await-async-events": [ | 
|  | 112 | +		2, | 
|  | 113 | +		{ | 
|  | 114 | +			"eventModule": "userEvent" | 
|  | 115 | +		} | 
|  | 116 | +	] | 
|  | 117 | +} | 
|  | 118 | +``` | 
|  | 119 | + | 
|  | 120 | +```json | 
|  | 121 | +{ | 
|  | 122 | +	"testing-library/await-async-events": [ | 
|  | 123 | +		2, | 
|  | 124 | +		{ | 
|  | 125 | +			"eventModule": "fireEvent" | 
|  | 126 | +		} | 
|  | 127 | +	] | 
|  | 128 | +} | 
|  | 129 | +``` | 
|  | 130 | + | 
|  | 131 | +```json | 
|  | 132 | +{ | 
|  | 133 | +	"testing-library/await-async-events": [ | 
|  | 134 | +		2, | 
|  | 135 | +		{ | 
|  | 136 | +			"eventModule": ["fireEvent", "userEvent"] | 
|  | 137 | +		} | 
|  | 138 | +	] | 
|  | 139 | +} | 
|  | 140 | +``` | 
|  | 141 | + | 
|  | 142 | +## When Not To Use It | 
|  | 143 | + | 
|  | 144 | +- `userEvent` is below v14, before all event methods are async | 
|  | 145 | +- `fireEvent` methods are sync for most Testing Library packages. If you are not using Testing Library package with async events, you shouldn't use this rule. | 
|  | 146 | + | 
|  | 147 | +## Further Reading | 
|  | 148 | + | 
|  | 149 | +- [Vue Testing Library fireEvent](https://testing-library.com/docs/vue-testing-library/api#fireevent) | 
0 commit comments