jeudi 11 février 2021

How do I test a component with a module dependency in Nuxt?

I have a Vue application that I've built on top of Nuxt. I make heavy use of DayJS and have installed the @nuxtjs/dayjs Nuxt module which binds this.$dayjs to the Vue instance and $dayjs to the Nuxt context.

I have built a component that takes advantage of the module, using this.$dayjs in several places. I want to test this component using Jest but even the following simple mounting test...

describe('Timeline component', () => {
  const wrapper = mount(Timeline)

  it('renders a Vue instance', () => {
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

...yields this error

TypeError: this.$dayjs is not a function

This makes sense. Normally, when testing Vue components you have to add your plugins to a local Vue instance.

const localVue = createLocalVue()
localVue.use(dayjs)

However, I'm not sure how to do this when the plugin is part of a Nuxt component.

Is there an official way of importing the Vue plugin portion of a Nuxt module to use in tests?

Aucun commentaire:

Enregistrer un commentaire