within(screen.getByRole('row', { name: /invoice 42/i })).getByRole('button', { name: 'Delete' })The within option is best. within limits queries to one element, so you target the button the way a user would: the Delete button in invoice 42's row. The tempting getAllByRole(...)[3] option works, but it breaks when the row order changes. The queryByRole and findByRole options fail too. They also throw or reject when several elements match.
Read more
- About queries (within) (https://testing-library.com/docs/queries/about)testing-library.com
- ByRole (https://testing-library.com/docs/queries/byrole)testing-library.com