---
title: "Discussion on \"How to test a select element with React Testing Library\" | Hashnode"
description: "Discussion on \"How to test a select element with React Testing Library\". Learn how to test a select/dropdown element with React Testing Library. We focus on writing tests the way a real user would interact with your application."
url: "https://hashnode.com/posts/how-to-test-a-select-element-with-react-testing-library/615deda08e8734462af5b4a6"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to test a select element with React Testing Library](https://cathalmacdonnacha.com/how-to-test-a-select-element-with-react-testing-library?utm_source=hashnode&utm_medium=feed)

Learn how to test a select/dropdown element with React Testing Library. We focus on writing tests the way a real user would interact with your application.

Oct 6, 2021 · 3 min read · cathalmacdonnacha.com  
Updated Nov 27, 2024  
Read the article

-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#testing-library](https://hashnode.com/tag/testing-library)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#frontend-development](https://hashnode.com/tag/frontend-development)
-   [#beginners](https://hashnode.com/tag/beginners)

## Written by

[Cathal Mac Donnacha](https://hashnode.com/@cathalmacdonnacha)

@cathalmacdonnacha · Ireland 🇮🇪

  
[View profile](https://hashnode.com/@cathalmacdonnacha)

Follow me on my journey as I write about tech topics I face in my everyday life as a Frontend Developer 💻

## Responses 3

Markdown supported · ⌘↵ to post

[Ákos Kőműves](https://hashnode.com/@akoskm)

[Nov 28, 2022](https://hashnode.com/posts/how-to-test-a-select-element-with-react-testing-library/615deda08e8734462af5b4a6/comment/6384d1eb9537c92102ec0fc5)

Thanks for sharing this [Cathal Mac Donnacha](https://hashnode.com/@cathalmacdonnacha).

I didn't know I could do

```
expect(screen.getByRole('option', { name: 'Select a country' }).selected).toBe(true)
```

[Cathal Mac Donnacha](https://hashnode.com/@cathalmacdonnacha)

[Nov 28, 2022](https://hashnode.com/posts/how-to-test-a-select-element-with-react-testing-library/615deda08e8734462af5b4a6/comment/6384d1eb9537c92102ec0fc5#reply-6384d8889537c92102ec108d)

Yea it's a much more reliable way of checking it. Glad you found it useful. 👍

[Satyam Tyagi](https://hashnode.com/@satyam305)

[May 28, 2022](https://hashnode.com/posts/how-to-test-a-select-element-with-react-testing-library/615deda08e8734462af5b4a6/comment/62926b7ed97f80b5091d9dec)

Getting error in -> ● should allow user to change country.

```
 FAIL  src/components/App.test.js
  ● should allow user to change country

    expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

      27 |     screen.getByRole('option', {name: 'Ireland'}),
      28 |   )
    > 29 |   expect(screen.getByRole('option', {name: 'Ireland'}).selected).toBe(true)
         |                                                                  ^
      30 | })
      31 |
```

[Cathal Mac Donnacha](https://hashnode.com/@cathalmacdonnacha)

[May 30, 2022](https://hashnode.com/posts/how-to-test-a-select-element-with-react-testing-library/615deda08e8734462af5b4a6/comment/62926b7ed97f80b5091d9dec#reply-6294b56a22c65adc00272426)

Hey, is this in the codesandbox test? Seems to be working fine for me.

[ankur dhull](https://hashnode.com/@dhull123)

[May 24, 2022](https://hashnode.com/posts/how-to-test-a-select-element-with-react-testing-library/615deda08e8734462af5b4a6/comment/628d04f8bc26026fa45c46fd)

screen.getByRole('option', { name: 'Ireland' }), for this getting error as multiple elements found out with role options. Throwing error at getByRole not going to the name condition

[Cathal Mac Donnacha](https://hashnode.com/@cathalmacdonnacha)

[May 25, 2022](https://hashnode.com/posts/how-to-test-a-select-element-with-react-testing-library/615deda08e8734462af5b4a6/comment/628d04f8bc26026fa45c46fd#reply-628dfd3419bda58ac7fb0855)

Hey [ankur dhull](https://hashnode.com/@dhull123)

Thanks for reading.

When it comes to select element options, I wouldn't expect there to be any duplicates (e.g options with the same name). If you put a live example up on codesandbox or somewhere similar I can take a look.

Cheers, Cathal.
