score:6
I had similar issue using Create React App setup.
If that is your case, you can create a file in your root directory called setupTest.js
and add the following code:
import '@testing-library/jest-dom/extend-expect';
import 'jest-extended';
jest.mock('./hooks/useResizeObserver', () => () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
})),
}));
You can find more information to configure the test environment for Create React App here and the ResizeObserver API here
score:0
Building upon the already excellent answers, here is what I did to get my React Testing library tests running
Take a dependency on the required polyfill in package.json
"devDependencies": {
...
"resize-observer-polyfill": "^1.5.1",
...
}
Update the setupTests.ts
file as follow.
import * as ResizeObserverModule from 'resize-observer-polyfill';
(global as any).ResizeObserver = ResizeObserverModule.default;
Now your tests should run fine.
score:5
I've added to setupTests.js/ts next code:
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}))
score:20
Mock the ResizeObserver:
class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
}
window.ResizeObserver = ResizeObserver;
export default ResizeObserver;
sample.test.js
import ResizeObserver from './__mocks__/ResizeObserver';
import module from 'sample';
describe('module', ()=> {
it('returns an instance of ResizeObserver', () => {
// do something that uses the resize observer
// NOTE: The actual observe handler would not be called in jsdom anyway as no resize would be triggered.
// e.g.
expect(module.somethingThatReturnAReference to the resize observer).toBeInstanceOf(ResizeObserver);
});
});
score:29
I chose to add the polyfill as a dev dependency and add the following line to setupTests.js/ts:
global.ResizeObserver = require('resize-observer-polyfill')
Source: stackoverflow.com
Related Query
- How to mock ResizeObserver to work in unit tests using react testing library
- How to mock useRef using jest.mock and react testing library
- How to mock a parent component which passes props to child component using react testing library
- How to mock a nested component import in react js and test using react testing library
- How to mock a complex react component with props using Jest/ react testing library
- How to mock out sub-components when unit testing a React component with Jest
- How to properly test React Native Modals using Jest and Native Testing Library
- Unit test form submission with data using react testing library
- How to write test cases for tabs in material-ui using react testing library
- how to output current rendered component in react testing library unit test
- How to mock stencil web components by using react testing library?
- how to test Routes in react using jest and react testing library
- React TestUtils doesn't work with decorators or how to mock higher order functions using rewire
- How can i write a test using React Testing Library to check if hover changes the components style?
- How to Unit test the rendering of a child component in the parent component using react testing library?
- how to mock npm uniqid for react unit test when using Jest?
- How to test a function inside React functional component using jest.spyOn with React Testing Library
- How to test HOC using react testing library
- how to test button that call submit form using jest and react testing library
- How to test the value of the props of react component rendered using react testing library
- How can I mock a service within a React component to isolate unit tests in jest?
- How to get Component by dynamic testid using React Testing Library
- Mock function not being called using jest and testing library on react app
- How to avoid "act" warnings in a integration tests with React Testing Library
- React Testing Library / Redux - How to mock cookies?
- Testing window.location.href in React unit tests using Mocha, Karma
- How to Test React's Error Boundary using Jest and React Testing Library
- How to mock input change for filtering data based on input string using react testing library?
- Unit tests on react with Jest: how to mock an action from different feature?
- How to refer to conditional elements when unit testing React components using jest and enzyme
More Query from same tag
- In-component data fetching depending on cookie
- react-dom.development.js:55 Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys
- Deselect checkboxes based on dropdown select value in ProComponents React
- How to get this Formik logic to work like I want the submit Button to do what I want
- Add sub-routine for Office.js Javascript Word add-in using asynchronous execution context
- Pass down a function with same name in React
- onsubmit gets triggered on every form field change
- Append to infinite scroll in reactJs
- Trigger a cloud function every time I create a new user
- I'm trying to make a page navigation bar using express framework and pug library when I hit my endpoint demo page I got nothing on my screen. please h
- What email service should I use for my React+Node based Ecommerce Website?
- How to develop Reusable Bootstrap-like Components
- Method parameter: destructuring + keeping original parameter (ReactJS component)
- Got all button updated in reactjs onclick event
- Unable to get values of Pointer Fields values Parse + React
- React Using Hook set state undefined when trying delete each item
- Is it good practice to put functions inside useEffect?
- ReactJS component names must begin with capital letters?
- Toggle in using react-strap is opening all toggles at the same time
- how to remove whitespace from password validation in react
- How to test an onChange input event with enzyme and jest
- useEffect localStorage loop
- Why is rendering the parent component and the child trying to enter the child component
- How to render one component multiple times and show the result one after the other?
- Getting Markers to Show up on a Google Map after Promise is fulfilled?
- Adding an object to a react hook array causes it to turn null
- Skipping function keyword throws syntax error
- react@unknown can't be resolved to a satisfying range
- Individual input values won't change
- Wait for all promises to resolve before updating state and redirect