-
Notifications
You must be signed in to change notification settings - Fork 272
Description
Version
3.0.0
Reproduction link
https://github.com/aethr/vue-prevent-test
Steps to reproduce
Create a link with an a
tag with a @click.prevent='clickHandler'
event handler like:
methods: {
clickHandler(event) {
console.log('event.defaultPrevented', event.defaultPrevented)
}
},
Click the link in the browser, console will show:
event.defaultPrevented true
Create a jest test that will trigger a click on the link like:
it('does not try to follow a link', async () => {
const wrapper = shallowMount(HelloWorld);
wrapper.get('#link').trigger('click')
})
Run jest test that trigger a click on the same element with npm run test:unit
, console will show:
console.log src/components/HelloWorld.vue:6
event.defaultPrevented false
What is expected?
Using vue-test-utils
to trigger a click event on a link should result in the same behaviour as we see in the browser, with event.defaultPrevented
set properly.
What is actually happening?
Although the prevent
modifier handler is being called, somehow the event itself is not being modified so that defaultPrevented
is true.
My test case also includes an example of a basic jest test that sets up a similar example using pure jsdom, and we can see the correct event.defaultPrevented
value is set, so I don't think this is an issue with jsdom.
In the app where we first saw this behaviour, we're seeing an error during one of our tests where jsdom thinks navigation is being attempted because an element with @click.prevent
is causing navigation (but only in jsdom, not in the browser). We think it is probably related to this bug.