-
-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"ResizeObserver is not defined" error when running Jest #40
Comments
Hey 👋 There must be some way to patch the test environment. |
I am having the same issue with jest. |
I'll be happy to add a section to the Readme if someone comments a solution. |
I used polyfilled version, |
I managed to bypass the issue like that.
|
I work on a component library where we use the hook to enhance component functionality behind a prop. So if The hook can't be placed behind a browser support conditional - so even when The only solution for us is to have two components, Component and ComponentWithResize. Render one or the other depending on prop and support. A check for RO support in the hook would be very inexpensive and fix these issues. |
@tay1orjones that sounds to me like a different issue. |
I ran into the same problem, in that I'd like to use this hook with graceful degradation for IE. I put up #42 which does just this, though it still needs some tests added. |
I added a test to #42, it should be ready to go. |
@ZeeCoder yep, thats fair. #42 will solve that for us, thanks @GreenGremlin! |
In case it helps someone, I found that you can mock module to use jest.mock('use-resize-observer', () => {
return jest.requireActual('use-resize-observer/polyfilled');
}); |
@tay1orjones the latest alpha release (https://github.com/ZeeCoder/use-resize-observer/releases/tag/v6.2.0-alpha.1) will not instantiate the ResizeObserver, until there's an actual element it receives, so it should handle your case. I still think the original issue with Jest reported here is not something the library needs to adapt to, but rather the environment needs to be mocked or polyfilled. |
Another possible mock solution - create a minimal mock of
|
adding this line of code in the testing file fixed the error for me |
For those looking for a more configurable solution, the below worked for me:
module.exports = {
moduleNameMapper: {
'use-resize-observer': 'use-resize-observer/polyfilled'
}
}; |
Solution for vitest
|
Hi, I have tried above solution of bypassing, it worked thank you. class ResizeObserver { describe('Component', () => { |
|
Thanks, It really works. I am rendering ReactFlow in JEST. Surprisingly it works. |
global.ResizeObserver = class { |
directy in jest.setup.ts works well 👍 |
npm i -D resize-observer-polyfill In setupTests.ts
|
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.
The text was updated successfully, but these errors were encountered: