Skip to content
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

fix(emit): force event handling even with fake timers #1856

Merged
merged 1 commit into from Nov 12, 2022

Conversation

cexbrayat
Copy link
Member

Fixes #1854

I'm not 100% convinced this is the proper way to fix it, but I don't have a better idea at the moment.
Setting _vts to Date.now() + 1 tricks Vue into thinking it has to handle the event even if Date.now() is faked and always returns the same value https://github.com/vuejs/core/blob/5ee40532a63e0b792e0c1eccf3cf68546a4e23e9/packages/runtime-dom/src/modules/events.ts#L100-L104

I hope this doesn't introduce other subtle issues. It would be great to only do this if the test is running with a fake Date, but I don't know how we can find out reliably for jest or vitest or even a manually mocked Date object.

@netlify
Copy link

netlify bot commented Nov 11, 2022

Deploy Preview for vue-test-utils-docs ready!

Name Link
🔨 Latest commit 2fe61b7
🔍 Latest deploy log https://app.netlify.com/sites/vue-test-utils-docs/deploys/636f6ade827c430009ae025e
😎 Deploy Preview https://deploy-preview-1856--vue-test-utils-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

aethr
aethr approved these changes Nov 12, 2022
Copy link
Contributor

@aethr aethr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think this is a clever solution that should prevent others from ever having to worry about the fake timers issue, and everything will "just work" as expected. This was very similar to my first fix in my project's codebase when I discovered the issue, but I had to add it to every event using the second arg to trigger.

Although it's tied to behavior in vue internals that could change, I think that's fine since this is the official testing library for vue. The comment does a good job of explaining what the workaround is for, and if vue changes implementation it should be easy to find the code that needs to be understood to fix it if it breaks.

Nice work!

wrapper.setProps({ label: 'foo' })

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line with wrapper.setProps isn't needed, I forgot to remove it from an earlier version when I was tinkering with the repro.

@cexbrayat cexbrayat force-pushed the fix/1854-event-fake-timers branch from c442f76 to 2fe61b7 Compare Nov 12, 2022
Copy link
Collaborator

@freakzlike freakzlike left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok to me

@cexbrayat cexbrayat merged commit 9722d21 into vuejs:main Nov 12, 2022
8 checks passed
Copy link
Member

@lmiller1990 lmiller1990 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, what a hack. I don't know of a better way to do this but this feels pretty dirty. 🤷 If it works, it works, nice fix.

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

Successfully merging this pull request may close these issues.

Bug: event handlers not invoked when using jest.useFakeTimers or vi.useFakeTimers
4 participants