Questions tagged [react-testing]
116 questions
2
votes
0 answers
FileLoader in three.js gives ReferenceError: Request is not defined while unit testing using mocha
I am trying to write a unit test for my three js utility functions.
My Project uses CRA w TS, and mocha & chai w TS for unit testing.
GeometryUtils.spec.ts
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { Scene, Vector3…

Subhankar Pal
- 21
- 2
2
votes
0 answers
css variables not working with react testing library
What I am doing is testing the dark mode feature. There is a button to toggle dark mode. When it clicks, the onClick event will trigger and the css variables values will be changed to dark mode. But when i check the background color of an textarea,…

Muhammed Rahif
- 434
- 1
- 5
- 17
2
votes
0 answers
How to test if a method has thrown an error inside a react component with cypress
My React-Component looks like this (simplified a lot):
const prepareStuff = (
{/* some parameters */}
): void => {
doOtherStuff(
{/* some parameters */}
);
};
const doOtherStuff = (
{/* some parementers */}
): void => {
…

Codehan25
- 2,704
- 10
- 47
- 94
2
votes
1 answer
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 [optionValue, setOptionValue] = useState({ value:…

Khursheed Alam
- 23
- 1
- 5
2
votes
0 answers
How should I test the function passed as prop into the child component?
I'm writing test for the below component with React Testing Library. And when I checked the test coverage, it showed me that coverage for the function passed to the prop 'errorRender' has not been covered.
Below is my component :
import React from…

user3760959
- 457
- 1
- 6
- 18
2
votes
0 answers
TypeError: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node'
I am writing test case using react-testing-library
while running test case I am getting TypeError: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node'.
TypeError: Failed to execute 'contains' on 'Node': parameter 1 is not of…

Saurabh Singh
- 197
- 1
- 6
2
votes
0 answers
Jest Mock 3rd party TS module - Getting "module not found" error while mocking
I am getting "module not found" error everytime I mock material-ui-nested-menu-item module using jest and react-testing-library.
Versions used
jest: 25.5.4
ts-jest: 25.5.1
Code is similar to following snippet-
App.ts
import NestedMenuItem from…

Kamaljeet Singh
- 21
- 3
1
vote
0 answers
is it OK to put asserations in React testing library's waitFor()?
from the official docs https://testing-library.com/docs/dom-testing-library/api-async/ which uses:
await waitFor(() => expect(mockAPI).toHaveBeenCalledTimes(1))
but what if my codebase does have a bug which causes the api (mocked by mockAPI) not…

lch
- 125
- 7
1
vote
1 answer
Unable to test conditional rendering using react-testing-library and jest
When I try to test conditional rendering of my component I get fail as result, even though it should pass.
Here is my component:
import React, { useEffect, useState } from 'react';
export const Carousel = ({ skeleton }) => {
const [isLoading,…

Miriam
- 21
- 1
1
vote
1 answer
Add Test Cases to test out the React 18 Strict mode behaviour of mounting component twice
In React 18 strict mode, Component first mounts, unmount and remount again. I want to add a test case in my React app to test this behaviour.
I am using karma, jasmine frameworks in my application. Currently didn't find how can we mount. So using…

Kotana Sai
- 1,207
- 3
- 9
- 20
1
vote
1 answer
React-testing-library: getByTestId() or queryByTestId() not working, thros Unable to find an element by data-testid
component is like:
1
vote
1 answer
React Testing Library - Get by component type
I've recently started upgrading my current React 16 and 17 projects to React 18. Consequently, I had to change my testing library from Enzym to React Testing Library (RTL). I came across a scenario where I can't find an equivalent. I'm using…

Tushar Gupta
- 23
- 3
1
vote
0 answers
How to write test case for document.body.classList.add/remove in React useEffect with jest & enzyme
How do I write unit test for following?
useEffect(() => {
document.body.classList.add("overflow_hidden");
return () => {
document.body.classList.remove("overflow_hidden");
}
}, []);

Sushmit Sagar
- 1,412
- 2
- 13
- 27
1
vote
1 answer
How to most effectively test a react component with a nested child component with conditional props
So I’ve read posts and watched tutorials on using Enzyme to test that a nested component is rendered with the correct props. I’ve also read the seemingly contradictory recommendation in the official react testing docs guidelines against testing…

GNG
- 1,341
- 2
- 23
- 50
1
vote
1 answer
Mocked useState not triggering a rerender React Testing Library
Code Example
Hi, I have an unusual test case, it's not working the way I expect. I am testing a component that resides inside of a context in our app. One of the values being passed down by the context is a piece of state as well as the setter of…

CHeffernan087
- 205
- 1
- 3
- 15