Skip to main content

Command Palette

Search for a command to run...

TIL #0: Difference between query types in React Testing Library

Updated
1 min read
TIL #0: Difference between query types in React Testing Library
N

I am a self taught web developer owning experience of 12+ years in web development field.

In React Testing Library, it provides screen query methods like

screen.getByRole('button', {name: /submit/i})

There are 3 commands available get, query & find. The usage depends upon the use test case.

|         | No Match | 1 Match | 1+ Match | Await? |
| ------- | -------- | ------- | -------- | ------ |
| queryBy | null     | return  | throw    | No     |
| getBy   | throw    | return  | throw    | No     |
| findBy  | throw    | return  | throw    | Yes    |

getBy      : expect element to be in DOM
queryBy    : expect element not to be in DOM
findBy     : expect element to appear async

For examples:

expect(screen.getByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
const submitButton = await screen.findByRole('button', {name: /submit/i})

References:

77 views
V

Each level in the game geometry dash introduces a new gameplay style, such as a portal that alters your direction or gravity, or a gateway that turns your cube into an arrow that waves up and down the screen. The levels are designed in a retro style, with later levels featuring more modern and detailed design elements. You can also create your own levels with the level editor, which you can then share with other players.

More from this blog

W

Web Chronicles

12 posts

I'm a Senior Full Stack Developer with 10+ years of experience. I strive to be a humble team player and be approachable to receive and give feedback.