admin管理员组文章数量:1332394
No doubt that Suspense feature leads to a cleaner code base, but as tidy as it is, it turns to be hard to test. Specifically it is not well documented yet.
case:
Regular app generated by VUE CLI
- Tech stack: Vuex, Router, PWA, jest for unit testing
Challenge:
I made use of Suspense ponent as remended as follows:
<RouterView name="default" v-slot="{ Component, route }">
<transition :name="route.meta.transition" mode="out-in" :duration="300" :key="route.path">
<Suspense >
<template #default>
<ponent :is="Component" :key="route.path"/>
</template>
<template #fallback>
<div class="top-0 right-0 h-screen w-screen z-50 flex justify-center items-center">
<div class="animate-spin rounded-full h-32 w-32 border-t-2 border-b-2 border-yellow-700"></div>
</div>
</template>
</Suspense>
</transition>
</RouterView>
I have few routes and views:
- one of them is for Login view
// here is the gotcha, if ever I removed async from setup the test runs well otherwise it always returns empty vm.
async setup(){
const router = useRouter()
const route = useRoute()
const form = reactive(new Form({
username:'',
password:'',
}))
}
And my test suite as follows:
test('Shows login form', async () => {
let wrapper = mount(Login,{
// tried set global.stubs.transition to false
renderDefaultSlot: true // tried as well to move this attr to beforeEach hook
})
expect(wrapper.exists()).toBe(true) // passes
await nextTick()
// tried to flushPromises()
console.log(wrapper.vm) // always empty object {}
expect(wrapper.findAll('div')).toBeTruthy() // fails accordingly as it can't run helper methods to get the parentElement
})
Does any VUE veteran here can give a hint or workaround!
All open discussions on Github shows that I am not the only one stumbled on this issue, but for now it is just a discussion.
No doubt that Suspense feature leads to a cleaner code base, but as tidy as it is, it turns to be hard to test. Specifically it is not well documented yet.
case:
Regular app generated by VUE CLI
- Tech stack: Vuex, Router, PWA, jest for unit testing
Challenge:
I made use of Suspense ponent as remended as follows:
<RouterView name="default" v-slot="{ Component, route }">
<transition :name="route.meta.transition" mode="out-in" :duration="300" :key="route.path">
<Suspense >
<template #default>
<ponent :is="Component" :key="route.path"/>
</template>
<template #fallback>
<div class="top-0 right-0 h-screen w-screen z-50 flex justify-center items-center">
<div class="animate-spin rounded-full h-32 w-32 border-t-2 border-b-2 border-yellow-700"></div>
</div>
</template>
</Suspense>
</transition>
</RouterView>
I have few routes and views:
- one of them is for Login view
// here is the gotcha, if ever I removed async from setup the test runs well otherwise it always returns empty vm.
async setup(){
const router = useRouter()
const route = useRoute()
const form = reactive(new Form({
username:'',
password:'',
}))
}
And my test suite as follows:
test('Shows login form', async () => {
let wrapper = mount(Login,{
// tried set global.stubs.transition to false
renderDefaultSlot: true // tried as well to move this attr to beforeEach hook
})
expect(wrapper.exists()).toBe(true) // passes
await nextTick()
// tried to flushPromises()
console.log(wrapper.vm) // always empty object {}
expect(wrapper.findAll('div')).toBeTruthy() // fails accordingly as it can't run helper methods to get the parentElement
})
Does any VUE veteran here can give a hint or workaround!
All open discussions on Github shows that I am not the only one stumbled on this issue, but for now it is just a discussion.
https://github./vuejs/vue-test-utils-next/issues/108#issue-611802592
https://github./vuejs/vue-test-utils/issues/956
Share Improve this question edited Jan 10, 2021 at 19:14 Ahmed Shehab asked Jan 10, 2021 at 15:30 Ahmed ShehabAhmed Shehab 1,87718 silver badges25 bronze badges1 Answer
Reset to default 9After investigation wrote a little helper quoted from Github discussion above:
import {defineComponent,h,Suspense } from 'vue'
import { mount } from '@vue/test-utils'
import flushPromises from 'flush-promises';
const mountSuspense = async (ponent, options) => {
const wrapper = mount(defineComponent({
render() {
return h(Suspense, null, {
default: h(ponent),
fallback: h('div', 'fallback')
})
}}), ...options)
await flushPromises()
return wrapper
}
describe('App renderes', ()=>{
test('About page renders',async()=>{
const wrapper = await mountSuspense(About)
await console.log(wrapper.text()) // it works
})
})
本文标签: javascriptWhat is the proper way to test Vue3 async setup component with suspenseStack Overflow
版权声明:本文标题:javascript - What is the proper way to test Vue3 async setup component with suspense? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742283012a2446441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论