236 questions
3
votes
1
answer
6k
views
Recommended approach for route-based tests within routes of react-router
I'm using react-testing-library within a project of mine and am trying to write tests that validate in-app routing.
e.g. testing that a button on the AccessDenied page brings you back to the Home page....
18
votes
5
answers
27k
views
Simplest test for react-router's Link with @testing-library/react
I'm trying to understand how to best test that react-router behaves as expected with @testing-library/react.
The simplest test I can think of, is to verify that clicking a link changes the URL. I ...
20
votes
3
answers
43k
views
SyntaxError: Missing initializer in const declaration
I am writing a simple test using react-native-testing-library (my first steps with that library) in my react native expo app. But I am getting a confused error coming from somewhere inside react-...
9
votes
4
answers
42k
views
how to test button that call submit form using jest and react testing library
so I am trying to test that the onSubmit function is getting triggered if the button is clicked - the way im doing this is through testing the internals of the onSubmit function is getting calles (...
135
votes
4
answers
212k
views
How to mock react custom hook returned value?
Here is my custom hook:
export function useClientRect() {
const [scrollH, setScrollH] = useState(0);
const [clientH, setClientH] = useState(0);
const ref = useCallback(node => {
...
17
votes
1
answer
17k
views
How to test a responsive design?
I'm trying to test a responsive design where I hide some text (in spans) when the screen size is too narrow.
import React from 'react'
import './index.css'
function Welcome(props) {
return (
&...
9
votes
2
answers
29k
views
How can I test if a prop is passed to child?
My component looks something like this: (It has more functionality as well as columns, but I have not included that to make the example simpler)
const WeatherReport: FunctionComponent<Props> = ({...
0
votes
1
answer
2k
views
Redux mock store is not being updated on react test environment using Jest/RTL
I'm new on testing and I'm trying to implement a unit test to check if a counter is being correctly updated after a button is clicked. Everything works fine on dev and production environments, so I'm ...
92
votes
4
answers
34k
views
Difference between enzyme, ReactTestUtils and react-testing-library
What is the difference between enzyme, ReactTestUtils and react-testing-library for react testing?
The ReactTestUtils documentation says:
ReactTestUtils makes it easy to test React components in ...
75
votes
14
answers
88k
views
how to test react-select with react-testing-library
App.js
import React, { Component } from "react";
import Select from "react-select";
const SELECT_OPTIONS = ["FOO", "BAR"].map(e => {
return { value: e, label: e };
});
class App extends ...
30
votes
1
answer
31k
views
How to test useEffect with async function and setState inside
I have set up a github project to understand how to better test react (v 16.8.0) useEffect hook.
I make an api call to fetch data inside useEffect and I set the received data as state component ...
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. ...
22
votes
4
answers
17k
views
How to paste clipboard data using React Testing Library?
I'm trying to paste text that's already in my clipboard into a textbox, but I dont understand how to use "eventInit" to do this. I've read the documentation on how to paste text into a ...
15
votes
3
answers
18k
views
Cannot read property 'addListener' of undefined react-testing-library
I'm trying to test my antd application with react testing library, but I keep getting this error:
TypeError: Cannot read property 'addListener' of undefined
Im using a custom render but the error ...
11
votes
1
answer
44k
views
How to mock fetch when testing a React app?
I would like to test a small React web app where I use the global fetch method.
I tried to mock fetch in this way:
global.fetch = jest.spyOn(global, 'fetch').mockImplementation(endpoint =>
...
8
votes
1
answer
9k
views
MaterialUI + React Testing Library: Unit test Select MenuItem breaks after upgrading to version 4
I've got a unit test using Jest and React Testing Library that fills and submits a form. The problem is that after upgrading Material UI to version 4 my unit test fails to select an option. The error ...
254
votes
19
answers
299k
views
react-testing-library why is toBeInTheDocument() not a function
Here is my code for a tooltip that toggles the CSS property display: block on MouseOver and on Mouse Out display: none.
it('should show and hide the message using onMouseOver and onMouseOut events ...
107
votes
4
answers
228k
views
Get by HTML element with React Testing Library?
I'm using the getByTestId function in React Testing Library:
const button = wrapper.getByTestId("button");
expect(heading.textContent).toBe("something");
Is it possible / ...
94
votes
2
answers
55k
views
Screen vs. render queries
There are two ways to use queries using React Testing Library.
You can either use the queries returned by the render method:
import React from 'react'
import { render } from '@testing-library/react'
....
94
votes
3
answers
172k
views
Jest mock react context
I need some help understanding how one can test an application using React Context.
Here's my sample setup.
context.js
import React from 'react'
export const AppContext = React.createContext()
App....
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 ...
56
votes
3
answers
153k
views
React testing library how to use waitFor
I'm following a tutorial on React testing. The tutorial has a simple component like this, to show how to test asynchronous actions:
import React from 'react'
const TestAsync = () => {
const [...
47
votes
3
answers
18k
views
What is the difference between using `react-testing-library` and `cypress`?
So, react-testing-library is used for unit/integration testing, and cypress is used for e2e testing. However, both appear to do the same thing:
react-testing-library
Facilitates mocking
Tests as a ...
39
votes
3
answers
83k
views
How to fetch element with 'name' attribute in react-testing-library
I'm testing my react app with react testing library and jest.
And I want to know how to fetch element with 'name' attribute in react-testing-library.
For example, in the following page, is it possible ...
34
votes
5
answers
89k
views
Testing custom hook: Invariant Violation: could not find react-redux context value; please ensure the component is wrapped in a <Provider>
I got a custom hook which I want to test. It receives a redux store dispatch function and returns a function. In order to get the result I'm trying to do:
const { result } = renderHook(() => { ...