Reputation: 61
I'm just learning Nuxt with jest. My test.vue. just copied from https://nuxtjs.org/docs/2.x/features/data-fetching
<template>
<div>
<h1 class="page-title">Mountains</h1>
<p v-if="$fetchState.pending">Fetching mountains...</p>
<p v-else-if="$fetchState.error">An error occurred :(</p>
<div v-else>
<h1>Nuxt Mountains</h1>
<ul>
<li v-for="mountain of mountains">{{ mountain.title }}</li>
</ul>
<button @click="$fetch">Refresh</button>
</div>
</div>
</template>
<script>
export default {
data () {
return {
mountains: [],
}
},
async fetch () {
this.products = await fetch(
'https://api.nuxtjs.dev/mountains'
).then(res => res.json())
}
}
</script>
My test.spec.js
import { mount } from '@vue/test-utils'
import Test from '@/components/test.vue'
describe('Test', () => {
test('is a Vue instance', () => {
const wrapper = mount(Test)
const title = wrapper.find('.page-title')
expect(title.text()).toBe("Mountains")
})
})
While I run the npm run test I got this error. How can I fix this issue?
[Vue warn]: Error in render: "TypeError: Cannot read property 'pending' of undefined"
Upvotes: 3
Views: 4621
Reputation: 86
Currently there is no library to extend the nuxt component structure within vue-test-utils.
That being said you are able to to do some yourself quite easily using the mocks api.
mounted(Component, { mocks: { $fetchState: { pending: true, error: true, timestamp: Date.now() } } })
The really tricky part is that fetch is not found within the component. you can find it in options, wrapper.vm.$options.fetch()
but no real way to test as of yet...
Upvotes: 7