Questions tagged [jest-dom]
56 questions
39
votes
5 answers
How to mock navigator.clipboard.writeText() in Jest?
I have tried the following 4 options after looking at Jest issues and SO answers, but I am either getting TypeScript errors or runtime errors. I would really like to get option 1 (spyOn) working.
// ------ option 1 -----
// Gives this runtime error:…

Naresh
- 23,937
- 33
- 132
- 204
20
votes
2 answers
Why do I get the error "Received value must be an HTMLElement or an SVGElement" when using react-native?
I'm new to unit testing and I'm trying to render a component to learn more about the library.
I'm trying to follow this guide.
Component

theabrar
- 360
- 1
- 6
- 15
18
votes
1 answer
Set size of window in Jest and jest-dom and jsdom
I test the height of the window in Jest with jest-dom and jsdom.
With this code :
import '@testing-library/jest-dom'
describe('test window height'), () => {
test('test the height'), () => {
expect(window.innerHeight).toBe(150) // 150 is an…

Pepito
- 375
- 1
- 3
- 8
15
votes
2 answers
Got TypeError: expect(...).toBeInTheDocument is not a function even after proper setup
I use Create React App and already declare this on src/setupTests.js:
import '@testing-library/jest-dom';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
But every time I use…

Inovramadani
- 1,955
- 3
- 14
- 34
8
votes
6 answers
Jest-dom give the error "TypeError: expect(...).toHaveStyle is not a function"
I'm trying to use jest-dom to test a component's styles, and I'm having the error:
"TypeError: expect(...).toHaveStyle is not a function"
My component is a simple link with styled-components:
import styled from 'styled-components'
export const…

Julio
- 139
- 1
- 1
- 8
7
votes
1 answer
How can i write a test using React Testing Library to check if hover changes the components style?
I am testing my application and encountered a problem. When trying to test whether a row in my Dropdown component applies an effect on hover, I noticed I was not able to check elements for their background color, which I find odd.
Trying to use the…

UndisclosedCurtain
- 143
- 1
- 2
- 14
7
votes
3 answers
@testing-library/jest-dom not loading
I'm trying to introduce React Testing Library into Create React App project, and I'm hitting a strange problem. For one of my tests, I need to use jest-dom, but I can't seem to import it successfully.
I've yarn add'ed it, and can't think of what…

David Goldfarb
- 1,796
- 3
- 18
- 32
5
votes
2 answers
Making assertions with mocha and chai when using react-testing-library?
I would like to add React Testing Library to a project that uses mocha and chai for unit tests. I am a bit stumped when it comes to writing assertions, most examples make use of jest and jest-dom. I am really keen to use assertions like…

Frederick Duffield
- 123
- 1
- 1
- 8
5
votes
4 answers
React Testing Library - "messageParent" can only be used inside a worker
I'm testinng a react component using the RTL, and everytime I ran a test, I get,
"messageParent" can only be used inside a worker
**Here's the code
describe('Header', () => {
it('validates header component is rendered', () => {
const {…

napster499
- 59
- 1
- 5
3
votes
1 answer
Mock animate function in react-testing-library / jest
I am using animate function to animate height of a div. The code looks like this and happens in useLayoutEffect :
const animation = ref.current.animate(
{ height: [oldHeight, newHeight] },
{ duration: 100 }
);
And my…

Vijay P R
- 1,162
- 1
- 10
- 16
3
votes
1 answer
Testing with 'toHaveStyle' passes when expecting fail
I'm learning about testing and react. I'm having a little trouble understanding why a test is passing when it should not:
From App.js
From App.test.js
…

rodtrevizan
- 33
- 1
- 4
3
votes
1 answer
Property 'toBeInTheDocument' does not exist on type 'Matchers'
Trying to setup testing for a components library. I've tried many example and all similar threads on this with no luck.
My setupTests.ts file is correctly being loaded (proven with a console.log), and the library seems to be available as if I add…

xaunlopez
- 439
- 1
- 4
- 13
2
votes
2 answers
Trouble testing React component render with Jest and Testing-Library because there's no Document available to Jest
Context/Setup:
I'm trying to use Jest and React-Testing-Library to test the render of a React component , but when I run the test, the client that handles fetch throws an error because it's using document.querySelector() - but when Jest runs…

dave
- 21
- 3
2
votes
1 answer
The fireEvent.click() from @testing-library/angular not working for 'ion-button' component but works for normal button
I am developing an application with Ionic Angular. While I am testing with @testing-library/angular, the test is not working for ion-button but works for normal button field. What could be the problem?
fireEvent.click() event is not working for the…

Faiz Ahmed
- 396
- 6
- 13
2
votes
1 answer
jest: ReferenceError: KeyframeEffect is not defined
I'm recently learning how to write component tests with jest and the react testing library.
I have an animation in useLayoutEffect, the code works fine on the browser but has error in jest, the code and error message are as…

lugy90
- 81
- 1
- 5