diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts index e28c191a5..794578306 100644 --- a/src/baseWrapper.ts +++ b/src/baseWrapper.ts @@ -369,6 +369,13 @@ export default abstract class BaseWrapper if (this.element && !this.isDisabled()) { const event = createDOMEvent(eventString, options) + // see https://github.com/vuejs/test-utils/issues/1854 + // fakeTimers provoke an issue as Date.now() always return the same value + // and Vue relies on it to determine if the handler should be invoked + // see https://github.com/vuejs/core/blob/5ee40532a63e0b792e0c1eccf3cf68546a4e23e9/packages/runtime-dom/src/modules/events.ts#L100-L104 + // we workaround this issue by manually setting _vts to Date.now() + 1 + // thus making sure the event handler is invoked + event._vts = Date.now() + 1 this.element.dispatchEvent(event) } diff --git a/tests/trigger.spec.ts b/tests/trigger.spec.ts index abd870863..8293a3ca8 100644 --- a/tests/trigger.spec.ts +++ b/tests/trigger.spec.ts @@ -364,4 +364,31 @@ describe('trigger', () => { expect(wrapper.emitted().enter).toHaveLength(1) }) + + // https://github.com/vuejs/test-utils/issues/1854 + it('dispatches events even with fakeTimers', async () => { + const handlerSpy = vi.fn() + + const Component = defineComponent({ + setup() { + return { handlerSpy } + }, + template: ` +
+ + Ok + +
+ ` + }) + + vi.useFakeTimers() + const wrapper = mount(Component) + + expect(handlerSpy).not.toHaveBeenCalled() + + await wrapper.get('span').trigger('click') + + expect(handlerSpy).toHaveBeenCalled() + }) })