Skip to content

Spying on a method does not work #1641

Closed
@jf-niji

Description

@jf-niji

Subject of the issue

When I trigger a click event on a component when spying a function, the function is called, but the spy does not work

Steps to reproduce

Here is my component:

<template>
	<button class="close-btn no-select" @click="close" @keydown.esc="close" />
</template>

<script>
	export default {
		name: 'CoreCloseButton',

		methods: {
			close() {
				this.$emit('close')
			},
		},
	}
</script>

Here is my test:

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

import CoreCloseButton from "./CoreCloseButton"

describe("CoreCloseButton.vue", () => {
	it('Should call close method when click on button', async () => {
		const wrapper = shallowMount(CoreCloseButton)

                let spyClose = jest.spyOn(wrapper.vm, 'close')

                await wrapper.trigger('click')

                expect(spyClose).toBeCalled()
	})
})

Expected behaviour

The spy should be called

Actual behaviour

The spy is not called

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