@@ -1208,6 +1208,178 @@ ruleTester.run(RULE_NAME, rule, {
12081208 ` ,
12091209 } ) as const
12101210 ) ,
1211+ ...USER_EVENT_ASYNC_FUNCTIONS . map (
1212+ ( eventMethod ) =>
1213+ ( {
1214+ code : `
1215+ import userEvent from '${ testingFramework } '
1216+ test('unhandled promise from event method called from userEvent.setup() return value is invalid', () => {
1217+ const user = userEvent.setup();
1218+ user.${ eventMethod } (getByLabelText('username'))
1219+ })
1220+ ` ,
1221+ errors : [
1222+ {
1223+ line : 5 ,
1224+ column : 11 ,
1225+ messageId : 'awaitAsyncEvent' ,
1226+ data : { name : eventMethod } ,
1227+ } ,
1228+ ] ,
1229+ options : [ { eventModule : 'userEvent' } ] ,
1230+ output : `
1231+ import userEvent from '${ testingFramework } '
1232+ test('unhandled promise from event method called from userEvent.setup() return value is invalid', async () => {
1233+ const user = userEvent.setup();
1234+ await user.${ eventMethod } (getByLabelText('username'))
1235+ })
1236+ ` ,
1237+ } ) as const
1238+ ) ,
1239+ // This covers the example in the docs:
1240+ // https://testing-library.com/docs/user-event/intro#writing-tests-with-userevent
1241+ ...USER_EVENT_ASYNC_FUNCTIONS . map (
1242+ ( eventMethod ) =>
1243+ ( {
1244+ code : `
1245+ import userEvent from '${ testingFramework } '
1246+ test('unhandled promise from event method called from destructured custom setup function is invalid', () => {
1247+ function customSetup(jsx) {
1248+ return {
1249+ user: userEvent.setup(),
1250+ ...render(jsx)
1251+ }
1252+ }
1253+ const { user } = customSetup(<MyComponent />);
1254+ user.${ eventMethod } (getByLabelText('username'))
1255+ })
1256+ ` ,
1257+ errors : [
1258+ {
1259+ line : 11 ,
1260+ column : 11 ,
1261+ messageId : 'awaitAsyncEvent' ,
1262+ data : { name : eventMethod } ,
1263+ } ,
1264+ ] ,
1265+ options : [ { eventModule : 'userEvent' } ] ,
1266+ output : `
1267+ import userEvent from '${ testingFramework } '
1268+ test('unhandled promise from event method called from destructured custom setup function is invalid', async () => {
1269+ function customSetup(jsx) {
1270+ return {
1271+ user: userEvent.setup(),
1272+ ...render(jsx)
1273+ }
1274+ }
1275+ const { user } = customSetup(<MyComponent />);
1276+ await user.${ eventMethod } (getByLabelText('username'))
1277+ })
1278+ ` ,
1279+ } ) as const
1280+ ) ,
1281+ ...USER_EVENT_ASYNC_FUNCTIONS . map (
1282+ ( eventMethod ) =>
1283+ ( {
1284+ code : `
1285+ import userEvent from '${ testingFramework } '
1286+ test('unhandled promise from aliased event method called from destructured custom setup function is invalid', () => {
1287+ function customSetup(jsx) {
1288+ return {
1289+ foo: userEvent.setup(),
1290+ bar: userEvent.setup(),
1291+ ...render(jsx)
1292+ }
1293+ }
1294+ const { foo, bar: myUser } = customSetup(<MyComponent />);
1295+ myUser.${ eventMethod } (getByLabelText('username'))
1296+ foo.${ eventMethod } (getByLabelText('username'))
1297+ })
1298+ ` ,
1299+ errors : [
1300+ {
1301+ line : 12 ,
1302+ column : 11 ,
1303+ messageId : 'awaitAsyncEvent' ,
1304+ data : { name : eventMethod } ,
1305+ } ,
1306+ {
1307+ line : 13 ,
1308+ column : 11 ,
1309+ messageId : 'awaitAsyncEvent' ,
1310+ data : { name : eventMethod } ,
1311+ } ,
1312+ ] ,
1313+ options : [ { eventModule : 'userEvent' } ] ,
1314+ output : `
1315+ import userEvent from '${ testingFramework } '
1316+ test('unhandled promise from aliased event method called from destructured custom setup function is invalid', async () => {
1317+ function customSetup(jsx) {
1318+ return {
1319+ foo: userEvent.setup(),
1320+ bar: userEvent.setup(),
1321+ ...render(jsx)
1322+ }
1323+ }
1324+ const { foo, bar: myUser } = customSetup(<MyComponent />);
1325+ await myUser.${ eventMethod } (getByLabelText('username'))
1326+ await foo.${ eventMethod } (getByLabelText('username'))
1327+ })
1328+ ` ,
1329+ } ) as const
1330+ ) ,
1331+ ...USER_EVENT_ASYNC_FUNCTIONS . map (
1332+ ( eventMethod ) =>
1333+ ( {
1334+ code : `
1335+ import userEvent from '${ testingFramework } '
1336+ test('unhandled promise from setup reference in custom setup function is invalid', () => {
1337+ function customSetup(jsx) {
1338+ const u = userEvent.setup()
1339+ return {
1340+ foo: u,
1341+ bar: u,
1342+ ...render(jsx)
1343+ }
1344+ }
1345+ const { foo, bar: myUser } = customSetup(<MyComponent />);
1346+ myUser.${ eventMethod } (getByLabelText('username'))
1347+ foo.${ eventMethod } (getByLabelText('username'))
1348+ })
1349+ ` ,
1350+ errors : [
1351+ {
1352+ line : 13 ,
1353+ column : 11 ,
1354+ messageId : 'awaitAsyncEvent' ,
1355+ data : { name : eventMethod } ,
1356+ } ,
1357+ {
1358+ line : 14 ,
1359+ column : 11 ,
1360+ messageId : 'awaitAsyncEvent' ,
1361+ data : { name : eventMethod } ,
1362+ } ,
1363+ ] ,
1364+ options : [ { eventModule : 'userEvent' } ] ,
1365+ output : `
1366+ import userEvent from '${ testingFramework } '
1367+ test('unhandled promise from setup reference in custom setup function is invalid', async () => {
1368+ function customSetup(jsx) {
1369+ const u = userEvent.setup()
1370+ return {
1371+ foo: u,
1372+ bar: u,
1373+ ...render(jsx)
1374+ }
1375+ }
1376+ const { foo, bar: myUser } = customSetup(<MyComponent />);
1377+ await myUser.${ eventMethod } (getByLabelText('username'))
1378+ await foo.${ eventMethod } (getByLabelText('username'))
1379+ })
1380+ ` ,
1381+ } ) as const
1382+ ) ,
12111383 ] ) ,
12121384 {
12131385 code : `
0 commit comments