-
Notifications
You must be signed in to change notification settings - Fork 112
How to use with Nuxt.js #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi Rubén! This is quite a good question 🤔 I can't think of a way to test a page with Not sure there's a way for us to fix this, to be honest. You could try and use tools such as If you come up with any good idea, please do share it! 🙌 |
To be honest I didn't came up with a good solution, I just went ahead and used Cypress 🤷♂ |
Thanks for the update! Looks like the way I would've followed, too :) |
i did this and it kind of works.. but is there a better solution now?
|
This is what I came up with, I might end up converting it into a tiny package later.
notices how it merges Then you can use like, assuming your
|
Here's what I've used: test/nuxtMountUtils.jsimport { 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 Usageimport 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,
},
})
} |
There should a way to call vm methods in testing-library to properly work with nuxt or it should have something like nuxt option that will call fetch automatically like created |
Hi! Thanks for your input
I don't think this is likely to happen, as it might lead to tests that rely on implementation details.
This is interesting and worth exploring! Do you have an idea about what the API should look like? |
@afontcu there basically two hooks that is executed and needed. |
What could the API look like? render(Component, { fetch, asyncData }) where |
what for pass them ? I mean its look like more what i offered as first option e.g. to call vm method manually but via different syntax. |
Oh right, I see, forget my last post. I have some doubts (does it mean |
Here's what I've used for asyncData: test/nuxtMountUtils.jsimport { mount, shallowMount } from '@vue/test-utils'
// See https://github.com/testing-library/vue-testing-library/issues/92
export async function shallowMountWithAsyncData(component, options) {
options.mountFunction = shallowMount
return await mountWithAsyncData(component, options)
}
export async function mountWithAsyncData(
component,
{ mountFunction, asyncDataGlobal, config, route, ...options } = {}
) {
if (!mountFunction) {
mountFunction = mount
}
const asyncData = component.options.asyncData
if (typeof asyncData !== 'function') {
throw new TypeError('asyncData should be a function')
}
const originalGlobal = {}
for (const key of Object.keys(asyncDataGlobal)) {
originalGlobal[key] = global[key]
global[key] = asyncDataGlobal[key]
}
const data = await asyncData({ $config: config, route })
for (const key of Object.keys(asyncDataGlobal)) {
global[key] = originalGlobal[key]
}
const wrapper = mountFunction(component, {
...options,
mocks: { $route: route },
data: () => data,
})
return wrapper
} Example Usageimport Index from '@/pages/index.vue'
import { mountWithAsyncData } from '~/test/nuxtMountUtils'
describe('Index', () => {
const config = {
serviceBaseUrl: 'https://example.com/service',
}
const route = {
params: {
someParam: 'example',
},
}
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 mountWithAsyncData(Component, {
asyncDataGlobal: {
fetch,
},
config,
route,
})
} |
any update for this? 😬 |
Hello!
I'm trying to use
vue-testing-library
with Nuxt.js and I'm facing a problem withasyncData
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 thinkvm
is available invue-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 methodThank you!
The text was updated successfully, but these errors were encountered: