You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm mocking window.PointerEvent since jsdom does not support it:
window.PointerEvent=PointerEventFake;
Then I use fireEvent from @testing-library/dom to dispatch an event. fireEvent uses element.ownerDocument.defaultView to get the associated Window object (source 1). It then creates an event using the constructor on this obtained Window instance (source 2). In Vitest, defaultView yields a different object than window, so @testing-library/dom does not find my PointerEvent mock.
A workaround would be to assign the mock on document.defaultView:
document.defaultView.PointerEvent=PointerEvent;
But this is obscure and most developers don't know what defaultView is (they shouldn't for this use case IMHO).
Is there a particular reason why [DOM node].defaultView isn't identical to window? Or am I doing anything wrong?
PASS ./dom.test.js
● Console
console.log
mouse click!
at HTMLDivElement.log (dom.test.js:31:15)
PASS ./first.test.js
● Console
console.log
document.defaultView === window true
at Object.log (first.test.js:2:11)
console.log
element.ownerDocument.defaultView === window true
at Object.log (first.test.js:9:11)
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.482 s, estimated 1 s
Ran all test suites.
Describe the bug
I'm migrating tests from Jest to Vitest (both using jsdom) and stumbled upon this subtle difference between Jest and Vitest:
See https://github.com/molily/vitest-test2/blob/main/first.test.js
Output in vitest:
Output in jest:
In Vitest,
window
anddocument.defaultView
are two different Window instances. I would expect both to point to the same object.Why is this relevant? In my tests, I mock objects on
window
and expect that it affects all elements in the document.My concrete test uses
@testing-library/dom
to fire an event on an element: https://github.com/molily/vitest-test2/blob/main/dom.test.jsI'm mocking
window.PointerEvent
since jsdom does not support it:Then I use
fireEvent
from@testing-library/dom
to dispatch an event.fireEvent
useselement.ownerDocument.defaultView
to get the associated Window object (source 1). It then creates an event using the constructor on this obtained Window instance (source 2). In Vitest,defaultView
yields a different object thanwindow
, so@testing-library/dom
does not find myPointerEvent
mock.A workaround would be to assign the mock on
document.defaultView
:But this is obscure and most developers don't know what
defaultView
is (they shouldn't for this use case IMHO).Is there a particular reason why
[DOM node].defaultView
isn't identical towindow
? Or am I doing anything wrong?Reproduction
Example repo: https://github.com/molily/vitest-test2
yields
whereas
yields
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: