Skip to content

Instantly share code, notes, and snippets.

@maurer2
Created April 15, 2024 13:34
Show Gist options
  • Save maurer2/04ce44d16fcce9b93009d1e7a77b2e1b to your computer and use it in GitHub Desktop.
Save maurer2/04ce44d16fcce9b93009d1e7a77b2e1b to your computer and use it in GitHub Desktop.
More readable "within" query in RTL
const parent = within(screen.getByRole('list', { name: 'test' }));
expect(parent.getByRole('list')).toBeVisible();
expect(parent.getAllByRole('listitem')).toHaveLength(10);
or
const { getByRole, getAllByRole } = within(screen.getByRole('list', { name: 'test' }));
expect(getByRole('list')).toBeVisible();
expect(getAllByRole('listitem')).toHaveLength(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment