Questions about testing JavaScript code with the Testing Library utilities
Questions tagged [testing-library]
282 questions
4
votes
2 answers
ReferenceError: window is not defined, consider using the "jsdom" test environment (jest V28)
I would like some assistance in resolving this error.
Am using Jest V28.0.0.
Here's how my package.json's test script & devDependencies look like
"scripts":{
...,
"test": "jest --env=node --watchAll --coverage --verbose",
},
…

muriukialex
- 73
- 1
- 7
4
votes
0 answers
getting " No QueryClient set, use QueryClientProvider to set one " when testing a react query component with react testing library
I'm getting this error when testing a component that uses react query custom hook:
" No QueryClient set, use QueryClientProvider to set one "
this is my hook:
export default () => {
const loginMutation = useMutation(
async (payload: LoginDto)…

Erfan Chegini
- 61
- 1
- 3
4
votes
0 answers
Index file causing error with Jest when I import
I am getting an error when I import a file for testing.
This is the only line in the file:
import Cart from '../../components/cart/Cart';
This is the error:
TypeError: Cannot read properties of undefined (reading 'default')
27 |
28…

AdamHinckley
- 215
- 4
- 16
4
votes
0 answers
Svelte: how to spyOn functions specified in template using jest?
I have a Svelte component that imports a function and then provides that function as a click handler in the template. I would like to spyOn and mock this function using jest.
After mocking the function, any calls to functions that call the mocked…

Scott Beale
- 61
- 2
4
votes
1 answer
React jest snapshot only returning empty DocumentFragment
I am using React and writing a Modal component like so:
const MyModal: FC = memo((props) => {
return (
...
);
});
I am trying to test…

Niru
- 1,407
- 1
- 28
- 47
4
votes
1 answer
How to test that my input has a ref attribute?
I am using React Testing Library and I have a component that takes in a prop value for a ref that will be used within a .
const InputComp = (ref) =>
I am then passing a simple useRef() into this…

physicsboy
- 5,656
- 17
- 70
- 119
4
votes
0 answers
Testing multiple fetch requests in jest
I cannot find any articles or documentation on how to test multiple synchronous fetch requests in a useEffect. I am fetching data and then before second fetch starts I set a "loading data" tooltip on a button. Once the second fetch is complete the…

LazioTibijczyk
- 1,701
- 21
- 48
4
votes
1 answer
React Jest testing : TestingLibraryElementError: Unable to find an element
i can't get the test id within my jest test. I am new to react testing. I want to test this component and get back for example the h3 text what was given before.
Tells me:
"TestingLibraryElementError: Unable to find an element by:…

MrGrashopper
- 41
- 1
- 1
- 4
4
votes
1 answer
Can't get TreeView Icons and IconButton for Tests
I'm trying to test a component that has an endAdornment IconButton and other with a TreeView, but neither the IconButton and the ExpandIcon/CollapseIcon have good options to dispatch test events.
This is the TextField component that I'm…

mayconbelfort
- 195
- 1
- 5
- 17
4
votes
1 answer
How to add '@testing-library/jest-dom' globally?
I saw in a project using create-react-app a file named setupTest.js which just imports @testing-library/jest-dom and the tests seems to use the extented expect version, like .toBeInTheDocument.
I added the same file (setupTest.js) to my project but…

Rodrigo
- 135
- 4
- 45
- 107
4
votes
1 answer
Svelte testing library won't detect component updates
My component: