question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use with Nuxt.js

See original GitHub issue

Hello!

I’m trying to use vue-testing-library with Nuxt.js and I’m facing a problem with asyncData not being called.

I’ve been reading for a while, and some people call wrapper.vm.$options.asyncData() manually, but that feels super dirty to me, also I don’t think vm is available in vue-testing-library

Any idea of how can I do it? I ran out of ideas to be honest.

I’ve seen @afontcu post about testing api calls but it uses the created life cycle method

Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
msmsimondeancommented, Nov 23, 2020

Here’s what I’ve used:

test/nuxtMountUtils.js

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

export async function shallowMountWithFetch(component, options) {
  options.mountFunction = shallowMount
  return await mountWithFetch(component, options)
}

export async function mountWithFetch(
  component,
  { mountFunction, fetchGlobal, fetchMocks, fetchContext, ...options } = {}
) {
  if (!mountFunction) {
    mountFunction = mount
  }
  const wrapper = mountFunction(component, options)
  const fetch = wrapper.vm.$options.fetch
  if (typeof fetch !== 'function') {
    throw new TypeError('fetch should be a function')
  }
  const thisArg = { ...wrapper.vm.$data, ...fetchMocks }
  const originalGlobal = {}
  for (const key of Object.keys(fetchGlobal)) {
    originalGlobal[key] = global[key]
    global[key] = fetchGlobal[key]
  }
  await fetch.apply(thisArg, [fetchContext])
  for (const key of Object.keys(fetchGlobal)) {
    global[key] = originalGlobal[key]
  }
  delete thisArg.$config
  wrapper.setData(thisArg)
  return wrapper
}

Example Usage

import Index from '@/pages/index.vue'
import { mountWithFetch } from '~/test/nuxtMountUtils'

describe('Index', () => {
  const bodyJson = { /* fake response body goes here */ }
  const fetch = jest.fn(() => {
    expect(url).toBe('https://example.com/something')
    return Promise.resolve({
      json: () => Promise.resolve(bodyJson),
    })
  })
  let wrapper
  const createWrapper = async () => {
    wrapper = await mountWithFetch(Component, {
      fetchGlobal: {
        fetch,
      },
    })
  }
2reactions
Martin7mindcommented, Jun 18, 2020

i did this and it kind of works… but is there a better solution now?

export const mountWithAsyncData = async (component, options) => {
  let data
  if (component.asyncData) {
    const asyncDataReq = component.asyncData({ app: { $axios: axios } })
    await options.respondToAsyncData()
    data = await asyncDataReq
  }

  return mount(component, {
    data: () => data,
    ...options
  })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt - The Intuitive Vue Framework
Build your next Vue.js application with confidence using Nuxt. An open source framework making web development simple and powerful.
Read more >
What is Nuxt.js? - Vue School
Nuxt.js is a framework for creating Vue.js applications. Its goal is to help Vue developers take advantage of top-notch technologies, fast, easy and...
Read more >
What Is Nuxt.js? Learn More About the Intuitive Vue Framework
Nuxt.js is a server-side rendering framework built on Vue.js. It abstracts away most of the complex configuration involved in managing ...
Read more >
Nuxt.js: a Minimalist Framework for Creating Universal Vue.js ...
Simply put, Nuxt.js is a framework that helps you build server-rendered Vue.js applications easily. It abstracts most of the complex ...
Read more >
Free Nuxt.js Tutorial — Vue Mastery Course - Medium
In this Nuxt tutorial, a free lesson from the Vue Mastery course, we'll build an application together and learn about the folder structure ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found