Skip to content

参考

基于 @vue/test-utils v2.x 编写

速查

挂载 API

API说明
mount(C, options)完整挂载,返回 VueWrapper
shallowMount(C, options)浅挂载,子组件 stub 成占位
mount(C, { shallow: true })等价 shallowMount
flushPromises()等待所有挂起的 Promise
enableAutoUnmount(afterEach)每个测试后自动 unmount
RouterLinkStubRouterLink 占位 stub
config.global套件级默认挂载选项

挂载选项

选项说明
props / attrs传入 props / 透传属性
slots插槽内容
data()Options API 的 data(仅 Options API)
attachTo挂载到真实 DOM(isVisible 需要)
shallow浅挂载开关
global.plugins注入 Pinia / Vue Router 等
global.provideinject 依赖
global.mocksmock 全局属性
global.stubsstub 子组件
global.components/directives全局组件 / 指令

Wrapper 方法

方法返回说明
find(css) / get(css)DOMWrapper查 DOM(get 找不到抛错)
findAll(css)DOMWrapper[]所有匹配
findComponent(C)VueWrapper查组件
trigger(event)Promise触发事件(await)
setValue(v)Promise设置表单值(await)
setProps({})Promise更新 props(await)
text() / html()string文本 / HTML
exists() / isVisible()boolean是否存在 / 可见
classes() / attributes()class / 属性
props() / emitted()props / 自定义事件记录
vm / unmount()Vue 实例 / 卸载

官方资源