"ResizeObserver is not defined" error when running Jest

See original GitHub issue

Hello 👋

Using useResizeObserver crashes my app’s tests. Jest uses JSDom, which apparently doesn’t support the ResizeObserver API.

I know we can detect when Jest is running, but React doesn’t support conditionally calling hooks, so I don’t know how to prevent Jest from crashing. I think the fix has to be done inside the hook.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

42reactions
sawsanCScommented, Dec 10, 2021

adding this line of code in the testing file fixed the error for me global.ResizeObserver = require(‘resize-observer-polyfill’)

12reactions
selonercommented, Jun 16, 2020

I managed to bypass the issue like that.

import React from 'react';

import { render, fireEvent } from '@testing-library/react';

import { AuthProvider } from '../context/AuthProvider';
import LoginPage from './LoginPage';

const pass = 'pass';
const user = 'user';
class ResizeObserver {
  observe() {}
  unobserve() {}
}
describe('loginpage ', () => {
  window.ResizeObserver = ResizeObserver;
  test('should update username and password', async () => {
    const { findByPlaceholderText } = render(
      <AuthProvider>
        <LoginPage />
      </AuthProvider>,
    );
    const username = await findByPlaceholderText('Your username...');
    const password = await findByPlaceholderText('Password...');
    fireEvent.change(username, { target: { value: user } });
    fireEvent.change(password, { target: { value: pass } });
    expect(username.value).toBe(user);
    expect(password.value).toBe(pass);
  });
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: ResizeObserver is not defined - Stack Overflow
js to create a line chart. I am getting below error when I am running the tests. ReferenceError: ResizeObserver is not defined 290...
Read more >
Testing useResizeObserver hook with jest and react ... - Reddit
I am using it in a parent component like so - import useResizeObserver from '@react-hook/resize-observer'; export const Block: ...
Read more >
ResizeObserver() - Web APIs | MDN
The ResizeObserver constructor creates a new ResizeObserver object, which can be used to report changes to the content or border box of an ......
Read more >
referenceerror resizeobserver is not defined - 掘金
微信分享"Reference error wx is not defined"错误很有可能是jweixin-1.0.0.js与你其它某js冲突。 点赞; 评论. 丁七岁.
Read more >
How to mock ResizeObserver to work in unit tests using react ...
Now your tests should run fine. Joseph King 4769. score:5. I've added to setupTests.js/ts next code: global.ResizeObserver = jest.fn().
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found