Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue utils - Check if a method is being called with jest

I am doing some tests and I want to check if a method is called after I trigger a click in an element, but the test keeps saying that is wasn't called and I don't know why

Here is my test:

test('some test', () => {
        const somethingChanged= jest.spyOn(Component.methods, 'somethingChanged')

        const wrapper = mount(Component, {
            propsData: data

        })

        const element= wrapper.find('.c-element').trigger('click')

        expect(somethingChanged).toBeCalled()
    })

This keeps saying that the number of calls is 0 and I don't know what I'm doing wrong This method is triggered in the component so I know it works

like image 392
user9875 Avatar asked Sep 19 '25 20:09

user9875


1 Answers

As trigger documentation states,

Triggers an event asynchronously on the Wrapper DOM node.

It should be:

const element= wrapper.find('.c-element').trigger('click')
await wrapper.vm.$nextTick()
expect(somethingChanged).toBeCalled()

The use of await assumes that test function should be async.

like image 190
Estus Flask Avatar answered Sep 21 '25 13:09

Estus Flask