Jest Vue:可以为快照加载动态导入吗?
我正在尝试单元测试一个动态加载其子组件的VUE组件。问题是开玩笑 / vue utils似乎无法呈现它。有什么方法吗?
我的组件
<template>
<component :is="component" v-bind="props" />
</template>
<script>
const components = {
dog: () => import('./dog.vue'),
cat: () => import('./cat.vue')
}
export default {
props: { type: String }
computed: {
component() {
return components[this.type]
}
props() { ... }
}
}
</script>
这是我的测试
...
it('renders correctly', async () => {
const wrapper = mount(Component, { ... })
expect(wrapper.element).toMatchSnapshot()
})
...
,这是由此产生的快照文件,
// Jest Snapshot v1
exports[`Markdown Token renders correctly 1`] = `<!---->`;
谢谢:)
I'm trying to unit test a Vue component that dynamically loads it's child component. The problem is that Jest / Vue utils doesn't seem to be able to render it. Is there any way of doing this?
My component
<template>
<component :is="component" v-bind="props" />
</template>
<script>
const components = {
dog: () => import('./dog.vue'),
cat: () => import('./cat.vue')
}
export default {
props: { type: String }
computed: {
component() {
return components[this.type]
}
props() { ... }
}
}
</script>
This is my test
...
it('renders correctly', async () => {
const wrapper = mount(Component, { ... })
expect(wrapper.element).toMatchSnapshot()
})
...
And this is the resulting snapshot file
// Jest Snapshot v1
exports[`Markdown Token renders correctly 1`] = `<!---->`;
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么它不会在第一个加载周期内加载动态组件,但是您可以更新道具,以确保组件在检查快照之前会导入懒惰的组件。
在这些示例中,我试图重置
type
属性,以便在不久之后导入动态组件。VUE UTILS示例:
测试库示例:
更新:
最佳方法可能是在检查快照之前加载懒惰的组件:
I don't know why it doesn't load the dynamic component in the first loading cycle but you can update the props to ensure the component will import the lazy-loaded component before checking the snapshot.
In these examples, I am trying to reset the
type
property, so that the dynamic component will be imported soon after.Vue utils example:
Testing library example:
Update:
The best approach might be loading lazy-loaded components before checking the snapshots: