Skip to content

trigger click on native HTML element trigger method but spy does not catch it #1901

Closed
@jf-niji

Description

@jf-niji

Subject of the issue

I have a component with a simple link and I want to test that a click on it triggers a method. With Vue test util + jest, the method is called but not the spy.

Version of vue-test-utils : 1.2.0

Steps to reproduce

import { shallowMount } from '@vue/test-utils'

// The component to test
const LinkComponent = {
	template: '<a href="www.google.fr" _target="_blank" @click="trackClick">My link</a>',
	methods: {
		trackClick() {
			console.log('Track click')
		},
	},
}

it('Should call trackClick when link is clicked', async () => {
	const wrapper = shallowMount(LinkComponent)
	const spyTrackClick = jest.spyOn(wrapper.vm, 'trackClick')

	const link = wrapper.find('a')

	await link.trigger('click')

	expect(spyTrackClick).toBeCalled()
})

Expected behaviour

Spy should be called

Actual behaviour

console.log is called and displayed in console & spy is not called
I can reproduce this issue with a ,

& other.

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