-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Window event listener fires but spy does not count it #7202
Comments
Can you check if this really works on Jest? I don't think this would work because export const register = () => {
// window.addEventListener('message', listenerObj.listener);
window.addEventListener('message', (e) => listenerObj.listener(e));
}; |
Hi @hi-ogawa Thanks for the quick reply. The actual use case is more complex and I thought I boiled it down to what's in the sample. But I think you might be right and I just needed to step back a bit. I will look at this some more, including a jest version. |
I put together a jest project that does the same thing. https://stackblitz.com/edit/stackblitz-starters-idnbgrtd?file=src%2Fmain.spec.js This shows both tests passing. |
@nick-roth-sas Thanks for the follow up. Okay, I think the same pattern actually exists on jest if you run only the 2nd test. https://stackblitz.com/edit/stackblitz-starters-emriphcy?file=src%2Fmain.spec.js Probably, the test looks working when running two together only because first spy didn't get cleaned up. I'd assume that's not what you intended in the test. The difference between Vitest and Jest is whether calling twice I don't think the issue is on Vitest, so let me close the issue. |
Describe the bug
Trying to migrate a test from jest to vitest. I have a use case where we are listening for message events on window, and I want to verify calls to the listener with a spy.
The provided reproduction shows two scenarios. One where the I call
vi.spyOn
before the event listener is added and the second where event listener is added first. The listener is called and runs in both cases but the spy only counts if the spy was created before adding towindow
.This behavior seems unintuitive at best and like an error in the worst case.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-t28wegld?file=test%2Fbasic.test.ts
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: