Skip to content

@click.prevent not setting event.defaultPrevented in jest tests #218

@aethr

Description

@aethr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions