setup() not triggered when testing Vue Components (composition-api)

Viewed 252

I’m using vue 2.6 with @vue/compisiton-api.

import { createLocalVue, shallowMount } from '@vue/test-utils';
import VueCompositionApi from '@vue/composition-api';
import Page from '@/components/templates/Page.vue';
import Component from './Component.vue';

const localVue = createLocalVue();
localVue.use(VueCompositionApi);

jest.mock('@vue/composition-api', () => ({
  reactive: jest.fn().mockReturnValue({
    isFetchingData: true,
  }),
  computed: jest.fn(),
  watch: jest.fn(),
}));

describe('Component', () => {
  it('test', async () => {
    const wrapper = shallowMount(Component, {
      localVue,
      stubs: {
        Page,
      },
    });

    expect(wrapper.findComponent(Page).exists()).toBe(true);
  });
});

For some reason when I shallowMount Component the setup() method is not triggered. Is there any specific extra config needed for that?

Thanks in advance

1 Answers

That's an old question, but maybe someone will stumble upon it like me.

My solution was to register the composition-api plugin just like you do it with Vuex:

import VueCompositionAPI from '@vue/composition-api'

const localVue = createLocalVue()

localVue.use(VueCompositionAPI)

return shallowMount(YourComponent, {
  localVue,
  // [...]
})
Related