All Questions
Tagged with react-testing-library react-test-renderer
42 questions
0
votes
1
answer
59
views
How to test react component with jest?
I want to test the Sidebar component. When you click on the "ToggleButton" it should toggle sidebar by className, but when clicking, an error appears stating that the function was not found.....
1
vote
0
answers
104
views
How test a function that return JSX.Element and contains API request?
I have a custom function that show a user's card (returns JSX.Element) when the pointer is over the user's name. This component request user information using React Query, which I use for data-...
0
votes
0
answers
2k
views
Testing for certain text using Vitest and React-Testing-Library after useEffect
My Dashboard component which is the parent component of the TopCards component which in turn is parent component of the Card1 Component
import TopCards from "./Dashboard/TopCards";
const ...
3
votes
0
answers
214
views
Jest snapshot test throws "Cannot read properties of null (reading 'scrollWidth')"
I'm trying to write a snapshot test for one of the component in my app.
The component is built with some Antd components like Select.
i get the following error.
The tests
import React from 'react'
...
0
votes
0
answers
194
views
testing custom hook within a component scope throws TypeError not a function
when I run my unit tests to test componernts, I recieve errors similar to this one in multiple places:
TypeError: (0 , _fileName.useCustomHook) is not a function
when I test the hook itself alone it ...
1
vote
0
answers
527
views
Why in React testing library fireevent.change not working from parent component?
The component i am testing is taking onChange function from props. This is main function that has a child component SingleText:
<Box className={classes.formAddress}>
<SingleText
...
0
votes
1
answer
445
views
react jest test return error when test component with properties
I would like to know why my last test is failed -> it("cron input should have a passed Value in properties".
I tried to test my react component without and with properties,
the first two ...
1
vote
0
answers
262
views
Styled component testing for react using 'react-test-renderer' and react testing-library
I am trying to have snapshot for this component but I get error
import React from 'react';
// material
import { styled } from '@mui/material/styles';
import { Box, Paper, Typography, Container, Grid }...
2
votes
1
answer
2k
views
Jest Snapshot Testing Wait till Component Renders
I feel like I am missing something when it comes to testing React components with async fetch operations.
I have a following component like this...
export default function Page() {
const [result, ...
0
votes
1
answer
1k
views
@testing-library/react render Error: `target` and `targetRef` props are `undefined`, it' required to use one of them
I am trying to write a unit test for a custom react component that use the Dialog from @fluentui/react-northstar when I try to render the component from the test I get a error:
Error: `target` and `...
1
vote
0
answers
3k
views
custom hook testing: when testing, code that causes React state updates should be wrapped into act(...):
Following this tutorial https://www.richardkotze.com/coding/mocking-react-hooks-unit-testing-jest, but getting this error even though the test passes, why is this error occurring and is there ...
0
votes
1
answer
3k
views
How to test React Number Format
I have used React Number Format package here and I want to test its onChange event but I'm getting an error, can someone please help me with this. I have given my test code and component below. In the ...
0
votes
1
answer
2k
views
How to test tr(table row) has onclick and style={{ cursor: "pointer" }} in react testing library
<tbody data-testid="offers-list-tablebody">
{ offers.map((Offer) => (
<tr onClick={this.handleTermsAndConditionsBtnClick} style={{ cursor: "pointer&...
8
votes
3
answers
8k
views
Jest test passes but .. has console message You are trying to access a property or method of the Jest environment after it has been torn down
My test passes but has a console refrenceError:
You are trying to access a property or method of the Jest environment after it has been torn down.
Test:
import {render, screen, cleanup, fireEvent, ...
2
votes
1
answer
5k
views
How to write test cases for Select Box onChange Method in React Testing Library?
I have a Select Box and set the data in a state by the onChange event. Please have a look at the below code. Anyone can help me to write cases for this onChange event in React Testing Library.
const [...
0
votes
2
answers
15k
views
How to test react component with onClick function Jest/React testing library
I am trying to get to 100% coverage for my tests in one of them it highlights the onClick function which sets the state for a component: this is the component:
I have a button which has an onClick ...
1
vote
1
answer
12k
views
React testing library getByLabelText -Label with special characters
it('render the field with label having the special characters', () => {
expect(screen.getByTestId('foo')).toHaveTextContent('About(En)'); // works
expect(screen.getByLabelText(/About(En)/u))....
1
vote
0
answers
98
views
How I test an async function which fetch data from another function in React Component?
I am new to React Testing. I want to test an async function that fetches rick and Morty data from API in React Component.
CharacterInfo.js
import React,{ useEffect, useState } from 'react';
import { ...
0
votes
1
answer
3k
views
Why can react-test-renderer only findByType functional components in the same file in React?
Here is the functional component some-table.jsx I am testing:
import React from 'react';
const SomeTable = ({ somethings }) => (
<>
{somethings.map(something => {
return <...
1
vote
1
answer
2k
views
Invalid hook call. Using react testing library and react query
I'm confused why I'm getting this error, that there is an invalid hook call. I am trying to follow the simple example from: https://react-query.tanstack.com/guides/testing. to test this simple ...
4
votes
2
answers
1k
views
Error when running unit test "You should not use <Link> outside a <Router>"
I have a unit test here and I am performing a click event. I am getting the error:
Invariant failed: You should not use <Link> outside a <Router>
describe("when the menu icon has been ...
3
votes
0
answers
374
views
React testing: test that component renders child component
I have a react component that renders a bunch of child components.
I am trying to test that this component indeeds renders these components.
I rather avoid Enzyme, so I am trying to use @testing-...
-1
votes
1
answer
675
views
How to test function which call api?
How to test a function which call get API and output a response.
I tried but I am able to test initial data which is in state , but I want to know how to test the function which updates the initial ...
15
votes
1
answer
4k
views
React snapshot testing - react-test-renderer vs. react-testing-library
I am trying to do snapshot testing in my React app. I am already using react-testing-library for unit testing in general. However for snapshot testing I have seen different ways of doing it on the net,...
0
votes
2
answers
1k
views
Test suite fails to run because of an error in pure.js using react-testing-library/react-hooks
I am testing a react app with the react-testing-library. To use renderhooks, I had to add the '@testing-library/react-hooks' library. This library depends on another library "pure.js". ...
0
votes
1
answer
3k
views
How to write unit test for Material UI slider? "Error: Cannot read property 'addEventListener' of null" thrown when attempting to render component
Trying to test the Material-UI Slider with Reat-Test-Renderer gets an error: Uncaught [TypeError: Cannot read property 'addEventListener' of null]
Codesandbox Link
import React from "react";
...
1
vote
1
answer
3k
views
Is there any point for using react-test-renderer with react-testing-library?
I can't understand why some tutorials use react-test-renderer with react-testing-library,
As i understand, react-test-renderer is a library that can create a pure object from a react component and ...
12
votes
2
answers
37k
views
How can I check whether an element is visible with Jest?
I have a straight forward react-comp. which I want to test the styling depending on the react state. The comp looks like the following:
React-comp.
const Backdrop = ({ showBackdrop }) => {
...
2
votes
0
answers
125
views
Unable to cover unit test cases fo React components having props
I have a component with the below code:-
import React from "react";
import { Box, Grid } from "@material-ui/core";
import ArrowBackIosIcon from "@material-ui/icons/...
0
votes
0
answers
495
views
Unable to write unit test cases for FHIR client api calls
I hava component Componnent1 and inside the componentDidMount i am having the below code:-
const key1 = sessionStorage.getItem("key1");
FHIR.oauth2
.ready()
.then((client) =&...
0
votes
1
answer
933
views
React unit test for Context and Reducer
I have created a LauoutContext.js for my application needs. Also used router location to get the URL / pathname. And passing reducer to child.
JS File: LayoutContext.js
import React, { createContext, ...
0
votes
1
answer
198
views
How can I test axios itself with mocking it?
I've just started learning react unit testing and the whole mock thing is confusing for me and I couldn't understand it .
I'm using axios to fetch data and display it in app component here :
const [ ...
2
votes
1
answer
2k
views
How to Unit test the rendering of a child component in the parent component using react testing library?
I cannot post the original code here ,so i will try to explain what i want to achieve.
So,my component displays message.This message is part of a child component which is being rendered in the parent ...
1
vote
1
answer
1k
views
React test unable to find an element mocked with jest
I am trying to mock a react select component for using it in a test firing event. Below is my react select that I created using jest.
jest.mock("react-select", () => ({ options, onChange }) => ...
1
vote
1
answer
2k
views
How to get React Test Renderer instance in React Native Testing Library
I have a component with a lifecycle method added by the navigation library - React Native Navigation (https://wix.github.io/react-native-navigation/#/docs/Usage?id=screen-lifecycle).
I must call ...
8
votes
1
answer
2k
views
react-testing-library render VS ReactDOM.render
We used to work with reactDom.render for our testing.
We started having problems when trying to test function component. In these case, the tests continued before all hooks were processed.
I started ...
0
votes
0
answers
98
views
Writing tests with testing-library depends on implementation details of my UI library
I am using @testing-library to test my React project which uses Material-UI as it's main UI library. As @testing-library suggests, it's main philosophy is to test the application in the exact same way ...
0
votes
1
answer
2k
views
how to test input value controlled by Redux state using @testing-library/react and react-test-renderer?
Using @testing-library/react,I am trying to test whether the input value is being by fireEvent or not. But Facing a problem, its always returning blank.As the value is controlled by the redux store.
...
30
votes
2
answers
17k
views
react-test-renderer's create() vs. @testing-library/react's render()
I'm new to React and confused about all the testing libraries. I got my test code to work but it seems redundant to have to call create() from react-test-renderer in order to use its toMatchSnapshot()...
5
votes
1
answer
10k
views
Test unmounting with react-testing-library
I am confused by when/how a queried element get updated. For example:
I have a button that counts up. And when it counts to 3, it disappears.
import React, { useState } from "react";
export ...
1
vote
1
answer
169
views
React test: after update input the props value still not change
I have this code in test
let formatField = subject.find("input").at(0);
(formatField.props() as any).onChange({
currentTarget: { value: "Potato" }
});
The problem is after this action when I ...
2
votes
1
answer
3k
views
React Testing Library: Do I need a new test with new selectors for each test key?
I'm building a React app with TDD using React Testing Library.
TLDR: Selectors (e.g. toBeInTheDocument()) only seem to work once, after render. Is that right?
I made the app using CRA and here is my ...