Questions about testing JavaScript code with the Testing Library utilities
Questions tagged [testing-library]
282 questions
7
votes
2 answers
How to test react components props (expect component to be called with)
I need to test that a react component is called with opened={true} prop after an button click is fired. I am using testing-library ( testing-library/react + testing-library/jest-dom).
I mocked the Component using something like
import Component from…

Berci
- 2,876
- 1
- 18
- 28
7
votes
2 answers
Testing library React - expect multiple elements to pass a specified condition
I have multiple buttons in my component and all of them should be disabled.
const buttons = screen.getAllByRole('button');
expect(ALL BUTTONS).toHaveAttribute('disabled'); // I want something like this.
How can we write this in the most convenient…

Danny
- 883
- 8
- 33
7
votes
2 answers
How to wait for something NOT to happen in Testing Library?
I want to assert on a condition which I know will not be true immediately, but might be true after an async action, in which the test should fail.
Say I'm testing this counter component:
function Counter() {
const [value, setValue] = useState(1);
…

Tamlyn
- 22,122
- 12
- 111
- 127
6
votes
1 answer
Calling userEvent.setup() in beforeEach() - is it a good or bad pattern?
In a test suite that needs to call userEvent.setup() in multiple tests, sometimes I'll do the userEvent.setup() in a beforeEach and then just reference the same user var in multiple tests
let user: UserEvent
beforeEach(() => {
user =…

minorgod
- 652
- 6
- 7
6
votes
1 answer
What could cause "Unable to find a label with the text of" for an element that is present?
What could cause getByLabelText from testing-library to report that it can't find an element when the element is definitely there? The exact error I'm getting is
TestingLibraryElementError: Unable to find a label with the text of: Remove Item: Item…

Layup
- 81
- 1
- 2
6
votes
1 answer
React Testing Library: how to use screen in this case instead of container
in react testing library, as per the conventions, it is better to use screen than to destructure the methods from render.
I have a component, where I need to test the text that is rendered. The top level container in this component is a div, so…

gaurav5430
- 12,934
- 6
- 54
- 111
6
votes
2 answers
vue3 testing library - How to use globalProperties in tests
I am new to Vue and followed the recommendation to use vue testing library. The only issue is I can't seem to find a way to inject my code into globalProperties in render function.
Does anyone know of an example where I can inject or mock it…

devtime
- 83
- 1
- 4
6
votes
2 answers
How to test next/script with testing-library?
My react component renders a