Skip to content

How to mock ResizeObserver #13

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

Open
philly-vanilly opened this issue Feb 22, 2025 · 6 comments
Open

How to mock ResizeObserver #13

philly-vanilly opened this issue Feb 22, 2025 · 6 comments

Comments

@philly-vanilly
Copy link

I am getting ResizeObserver loop completed with undelivered notifications. in the tests. I am using vuetify and the recommendation is to add resize-observer-polyfill https://vuetifyjs.com/en/getting-started/unit-testing/#setup-vitest. When switching from regular to browser execution, this does not work anymore as global (outside node context) is not defined:

import resize_observer_polyfill from 'resize-observer-polyfill';
(global as any).ResizeObserver = resize_observer_polyfill;

other objects are defined but make no difference when you attach the ResizeObserver:

self.ResizeObserver = resize_observer_polyfill;
self.window.ResizeObserver = resize_observer_polyfill;
window.ResizeObserver = resize_observer_polyfill;
document.ResizeObserver = resize_observer_polyfill;

I tried assigning in test-setup.ts as defined in vitest.config as well as in beforeEach(...)

@sheremet-va
Copy link
Member

ResizeObserver exists in the browser, you don't need to polyfill it. The polyfill exists for the Node.js tests

@philly-vanilly
Copy link
Author

@sheremet-va Is there a way to ignore the error, then? Or to replace the real resize observer with a dummy? The error does not appear when I use the tested components in the actual app, just in the tests. I assume because the component is unmounted before resize observer listener is terminated?

@sheremet-va
Copy link
Member

I assume because the component is unmounted before resize observer listener is terminated?

Do you terminated it at all? Vitest doesn't release any resources

@philly-vanilly
Copy link
Author

@sheremet-va Yes, the tests terminate. They even pass, but with errors being thrown subsequently from the looks of it (see screenshots), causing the final result to fail:

Image Image Image

@sheremet-va
Copy link
Member

I'm asking if you terminate the observer (in unmount, for example)

https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/unobserve

@philly-vanilly
Copy link
Author

I'm asking if you terminate the observer (in unmount, for example)

https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/unobserve

I have no resize observer relevant code myself, I guess it is in a vue-date-picker component, which is an npm lib and outside my control. I have had similar issues with v-selects and v-autocompletes from vuetify in cypress tests and had to ignore them to keep the tests green. Is there a way to do this in a vitest-browser-vue setup with chromium and playwright?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants