等待 onMounted() 完成内部测试
Wait for onMounted() to finish inside test
在我的设置中,我在 onMounted
函数中执行了几个函数。在我的测试中,我想等待这些完成。我怎样才能做到这一点?
我尝试使用 nextTick
和 flushPromises
(即使没有任何承诺),但其中 none 有效。
这是一些示例代码:
Vue 组件:
setup() {
const mounted = ref(false);
onMounted(() => {
mounted.value = true;
})
}
测试:
describe('TestComponent', () => {
const wrapper = shallowMount(TestComponent)
it('expects mounted to be true after mount', () => {
expect(wrapper.vm.mounted).toBe(true)
})
})
缺少的是在设置组件后导出数据:
setup() {
const mounted = ref(false);
onMounted(() => {
mounted.value = true;
})
return { mounted };
}
在我的设置中,我在 onMounted
函数中执行了几个函数。在我的测试中,我想等待这些完成。我怎样才能做到这一点?
我尝试使用 nextTick
和 flushPromises
(即使没有任何承诺),但其中 none 有效。
这是一些示例代码:
Vue 组件:
setup() {
const mounted = ref(false);
onMounted(() => {
mounted.value = true;
})
}
测试:
describe('TestComponent', () => {
const wrapper = shallowMount(TestComponent)
it('expects mounted to be true after mount', () => {
expect(wrapper.vm.mounted).toBe(true)
})
})
缺少的是在设置组件后导出数据:
setup() {
const mounted = ref(false);
onMounted(() => {
mounted.value = true;
})
return { mounted };
}