3,737 questions
103
votes
4
answers
131k
views
Consider using the "jsdom" test environment
I have this simple test:
import React from 'react'
import { render } from '@testing-library/react'
import Button from '.'
describe('Button', () => {
it('renders button without crashing', () =&...
92
votes
18
answers
108k
views
React testing library on change for Material UI Select component
I am trying to test the onChange event of a Select component using react-testing-library.
I grab the element using getByTestId which works great, then set the value of the element and then call ...
3
votes
2
answers
10k
views
ReferenceError: crypto is not defined
I am testing my React app and when it runs a test that executes the Web Cryptography API, specifically await crypto.subtle.generateKey I get the following error message
ReferenceError: crypto is not ...
1
vote
2
answers
2k
views
Is there a way to test sliders using React Testing Library's `userEvent`?
The React Testing Library docs specify that userEvent is to be preferred to fireEvent. However, I can't figure out a userEvent way to click to a particular value on a slider. With fireEvent, we can do:...
89
votes
4
answers
107k
views
When to use userEvent.click and when to use fireEvent
I'd like to test mouse interaction with an element using React Testing Library. Presently it's a bit unclear to me the difference between userEvent.click(element) and fireEvent.click(element).
Are ...
4
votes
1
answer
10k
views
React-Testing-Library can't find component with given role and name
I have an input with a dynamically-generated name that I would like to select with react-testing-library. RTL doesn't detect the input, even though the CLI error output contains an element with the ...
0
votes
1
answer
76
views
testing an useState change inside a callback
I have a component like this to test:
import {doSomethingMocked} form 'mockedLibrary';
const TheComponent = (props) => {
const [isToTest, setIsToTest] = useState(false);
const handleClick = ...
4
votes
2
answers
21k
views
How to get the elements from a table after all components are rendered?
I'm trying to test my UsersTable page in React, but the testing library gets the elements before they have time to render.
Please check my code below:
UsersTable.js
import React, { useState, useEffect ...
21
votes
2
answers
25k
views
TypeError: Expected container to be an Element, a Document or a DocumentFragment but got string
I'm trying to run the below testcase using RTL , and the test is failing with the error "TypeError: Expected container to be an Element, a Document or a DocumentFragment but got string.". I ...
0
votes
1
answer
11k
views
Change a state value in a React testing library
I have the following component:
import { useState } from 'react';
export function MyComponent() {
const [val, setVal] = useState(false);
return (
<div>
big container
<...
0
votes
1
answer
2k
views
React Testing Library doesn't see click on submit button
I am struggled with writing tests to my simple form:
import { useFormik } from 'formik';
import * as yup from 'yup';
import Button from '@mui/material/Button';
import TextField from '@mui/material/...
2
votes
3
answers
4k
views
React Testing Library - Global configuration [closed]
I need to override default testID attribute in React Testing Library. Using Jest as a testing framework.
For this, I have followed the steps given here at individual test file level.
Is there any way ...
6
votes
4
answers
10k
views
Jest tests are failing because of an unknown unexpected token "export"
I'm using tRPC with Typescript and Next.js. My tests used to not fail until recently (I don't think I touched them). I get the error Unexpected token 'export'. I've tried changing my jest.config.mjs ...
0
votes
1
answer
32
views
How to Assert the Absence of an Element After an Async API Response in Testing Library
I'm wondering if it's better to use findBy together with rejects.toThrow instead of using queryBy with not.toBeInTheDocument when checking that an element is not displayed in cases like the following. ...
1
vote
1
answer
74
views
How to make NextJS 15 dynamic CSS generation blank?
Earlier I was using NextJS 12 version & we stop generating the dynamic CSS class while writing the snapshot in React Testing Library by using the below method:
mocks\styled-jsx\css.js
function css(...
13
votes
2
answers
19k
views
Cannot find module '@testing-library/dom' from 'node_modules/@testing-library/user-event/dist/click.js' Require stack:
In a project we recently started using userEvent instead of fireEvent but now we are getting this issue
FAIL src/modules/DashboardHeader/index.test.tsx
● Test suite failed to run
...
0
votes
1
answer
182
views
Vitest react wait for function in useCallback to be in ready state
I have the following hook:
function useWelcomeMessage() {
const { data, error} = useGetWelcomeMessage();
const printMessage = useCallback(async () => {
if (data) {
alert(data)
...
1
vote
3
answers
8k
views
Is it okay to use the expect inside of a waitFor as my "Assert" in Act-Arrange-Assert?
I've recently begun utilizing React / DOM Testing Library in order to test all of my front-end React code. One of the first issues I've encountered is that with the way our application is laid out, I ...
1
vote
0
answers
87
views
What exactly are the conditions that React will give the act warning?
I know that this is a well trodden topic, but most of the articles and help topics on this talk about how to avoid, and don't actually detail what exactly causes it.
I have a simple example of how we ...
14
votes
3
answers
3k
views
How to test Material UI v5 components with sx props in @testing-library/react?
React Testing Library does not apply sx props of Material UI components during rendering.
For example, I specify properties to hide an element at certain breakpoints.
<>
<AppBar
data-...
43
votes
9
answers
58k
views
Pressing enter to submit form in react-testing-library does not work
Description:
I am trying to test that a form submits when the user presses the "Enter" key. I have a passing test for when pressing the Submit button, but I also want to be sure the form submits with ...
2
votes
2
answers
2k
views
Mui DatePicker button not available on first render in tests
The new Material UI DatePicker has a renderInput prop that gets a function that renders a text field. Works well enough, except that this function is rendered twice, and on first render it only ...
72
votes
13
answers
63k
views
How to test Material UI autocomplete with react testing library
I am using Material UI autocomplete component and am trying to test it using react-testing-library
Component:
/* eslint-disable no-use-before-define */
import TextField from '@material-ui/core/...
24
votes
4
answers
13k
views
react-testing-library why use test id
I am using react-testing-library. In the document it says
Note that using this as an escape hatch to query by class or id is a
bad practice because users can't see or identify these attributes. ...
0
votes
1
answer
193
views
How can one test Chakra v3 UI Select in NextJS with Jest?
I'm building a NextJS (react) app with Chakra UI (version 3). I'd like to write unit tests, but I don't know to handle the way Chakra UI v3 handles select.
Here's a sample test app:
'use client';
...