vue-test-utils-skilld
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesevuejs/test-utils @vue/test-utils
@vue/test-utilsvuejs/test-utils @vue/test-utils
@vue/test-utilsVersion: 2.4.6 (May 2024)
Deps: js-beautify@^1.14.9, vue-component-type-helpers@^2.0.0
Tags: latest: 2.4.6 (May 2024), 2.0.0-alpha.0: 2.0.0-alpha.0 (Apr 2020), 2.0.0-alpha.1: 2.0.0-alpha.1 (Apr 2020), 2.0.0-alpha.2: 2.0.0-alpha.2 (Apr 2020), 2.0.0-alpha.3: 2.0.0-alpha.3 (Apr 2020), 2.0.0-alpha.4: 2.0.0-alpha.4 (May 2020), next: 2.4.0-alpha.2 (Jun 2023), 2.0.0-alpha.8: 2.0.0-alpha.8 (Jul 2020), 2.0.0-beta.1: 2.0.0-beta.1 (Aug 2020), 2.0.0-beta.2: 2.0.0-beta.2 (Aug 2020), 2.0.0-beta.3: 2.0.0-beta.3 (Aug 2020), 2.0.0-beta.4: 2.0.0-beta.4 (Sep 2020), 2.0.0-beta.5: 2.0.0-beta.5 (Sep 2020), 2.0.0-beta.7: 2.0.0-beta.7 (Oct 2020), 2.0.0-beta.8: 2.0.0-beta.8 (Nov 2020), 2.0.0-beta.9: 2.0.0-beta.9 (Nov 2020), 2.0.0-beta.10: 2.0.0-beta.10 (Nov 2020), 2.0.0-beta.12: 2.0.0-beta.12 (Dec 2020), 2.0.0-beta.13: 2.0.0-beta.13 (Dec 2020), 2.0.0-rc.0: 2.0.0-rc.0 (Jan 2021), 2.0.0-rc.1: 2.0.0-rc.1 (Feb 2021), 2.0.0-rc.2: 2.0.0-rc.2 (Feb 2021), 2.0.0-rc.3: 2.0.0-rc.3 (Mar 2021), 2.0.0-rc.4: 2.0.0-rc.4 (Mar 2021), 2.0.0-rc.5: 2.0.0-rc.5 (Apr 2021), 2.0.0-rc.6: 2.0.0-rc.6 (Apr 2021), 2.0.0-rc.7: 2.0.0-rc.7 (Jun 2021), 2.0.0-rc.8: 2.0.0-rc.8 (Jun 2021), 2.0.0-rc.9: 2.0.0-rc.9 (Jun 2021), 2.0.0-rc.10: 2.0.0-rc.10 (Jul 2021), 2.0.0-rc.11: 2.0.0-rc.11 (Jul 2021), 2.0.0-rc.12: 2.0.0-rc.12 (Jul 2021), 2.0.0-rc.14: 2.0.0-rc.14 (Sep 2021), 2.0.0-rc.16: 2.0.0-rc.16 (Oct 2021), 2.0.0-rc.18: 2.0.0-rc.18 (Dec 2021), legacy: 1.3.6 (Jun 2023), 2.4.0-alpha.0: 2.4.0-alpha.0 (Apr 2023), v2.4.0-alpha.2: 2.4.0-alpha.2 (Jun 2023)
References: Docs — API reference, guides • GitHub Issues — bugs, workarounds, edge cases • Releases — changelog, breaking changes, new APIs
**版本:**2.4.6(2024年5月)
**依赖:**js-beautify@^1.14.9, vue-component-type-helpers@^2.0.0
**版本标签:**latest: 2.4.6(2024年5月), 2.0.0-alpha.0: 2.0.0-alpha.0(2020年4月), 2.0.0-alpha.1: 2.0.0-alpha.1(2020年4月), 2.0.0-alpha.2: 2.0.0-alpha.2(2020年4月), 2.0.0-alpha.3: 2.0.0-alpha.3(2020年4月), 2.0.0-alpha.4: 2.0.0-alpha.4(2020年5月), next: 2.4.0-alpha.2(2023年6月), 2.0.0-alpha.8: 2.0.0-alpha.8(2020年7月), 2.0.0-beta.1: 2.0.0-beta.1(2020年8月), 2.0.0-beta.2: 2.0.0-beta.2(2020年8月), 2.0.0-beta.3: 2.0.0-beta.3(2020年8月), 2.0.0-beta.4: 2.0.0-beta.4(2020年9月), 2.0.0-beta.5: 2.0.0-beta.5(2020年9月), 2.0.0-beta.7: 2.0.0-beta.7(2020年10月), 2.0.0-beta.8: 2.0.0-beta.8(2020年11月), 2.0.0-beta.9: 2.0.0-beta.9(2020年11月), 2.0.0-beta.10: 2.0.0-beta.10(2020年11月), 2.0.0-beta.12: 2.0.0-beta.12(2020年12月), 2.0.0-beta.13: 2.0.0-beta.13(2020年12月), 2.0.0-rc.0: 2.0.0-rc.0(2021年1月), 2.0.0-rc.1: 2.0.0-rc.1(2021年2月), 2.0.0-rc.2: 2.0.0-rc.2(2021年2月), 2.0.0-rc.3: 2.0.0-rc.3(2021年3月), 2.0.0-rc.4: 2.0.0-rc.4(2021年3月), 2.0.0-rc.5: 2.0.0-rc.5(2021年4月), 2.0.0-rc.6: 2.0.0-rc.6(2021年4月), 2.0.0-rc.7: 2.0.0-rc.7(2021年6月), 2.0.0-rc.8: 2.0.0-rc.8(2021年6月), 2.0.0-rc.9: 2.0.0-rc.9(2021年6月), 2.0.0-rc.10: 2.0.0-rc.10(2021年7月), 2.0.0-rc.11: 2.0.0-rc.11(2021年7月), 2.0.0-rc.12: 2.0.0-rc.12(2021年7月), 2.0.0-rc.14: 2.0.0-rc.14(2021年9月), 2.0.0-rc.16: 2.0.0-rc.16(2021年10月), 2.0.0-rc.18: 2.0.0-rc.18(2021年12月), legacy: 1.3.6(2023年6月), 2.4.0-alpha.0: 2.4.0-alpha.0(2023年4月), v2.4.0-alpha.2: 2.4.0-alpha.2(2023年6月)
参考链接:文档 — API参考、指南 • GitHub问题 — 问题、解决方案、边缘情况 • 版本发布 — 更新日志、破坏性变更、新API
API Changes
API变更
This section documents version-specific API changes — prioritize recent major/minor releases.
-
BREAKING:— v2 renamed to
propsDatafor consistency with component definitions sourceprops -
BREAKING:— removed in v2, use the
createLocalVuemounting option to install plugins, mixins, or directives sourceglobal -
BREAKING:and
mocks— moved into thestubsmounting option in v2 as they apply to all components sourceglobal -
BREAKING:— renamed to
destroy()in v2 to match Vue 3 lifecycle naming sourceunmount() -
BREAKING:— removed in v2;
findAll().at()now returns a standard array of wrappers sourcefindAll() -
BREAKING:— removed in v2, use the
createWrapper()constructor for non-component elements sourcenew DOMWrapper() -
BREAKING:— v2 no longer renders default slot content for stubbed components by default source
shallowMount -
BREAKING:— now only supports
find()syntax; usequerySelectorto locate Vue components sourcefindComponent() -
BREAKING:and
setSelected— removed in v2, functionality merged intosetCheckedsourcesetValue() -
BREAKING:— renamed to
attachToDocumentin v2 sourceattachTo -
BREAKING:— removed in v2, use
emittedByOrderinstead sourceemitted() -
NEW:— added in v2.3.0 to support SSR testing source
renderToString() -
NEW:/
enableAutoUnmount()— replacesdisableAutoUnmount()in v2 sourceenableAutoDestroy -
DEPRECATED:— removed in v2 and merged into the
scopedSlotsmounting option sourceslots
Also changed: and return · scope exposed as in string templates · , , , , , and removed
setValue()trigger()nextTickslotsparamsisisEmptyisVueInstancenamesetMethodscontains本节记录特定版本的API变更 — 优先关注近期的主要/次要版本更新。
-
破坏性变更:— v2版本中更名为
propsData,与组件定义保持一致 来源props -
破坏性变更:— v2版本中已移除,使用
createLocalVue挂载选项来安装插件、混入或指令 来源global -
破坏性变更:和
mocks— v2版本中移至stubs挂载选项,因为它们适用于所有组件 来源global -
破坏性变更:— v2版本中更名为
destroy(),以匹配Vue 3的生命周期命名 来源unmount() -
破坏性变更:— v2版本中已移除;
findAll().at()现在返回标准的包装器数组 来源findAll() -
破坏性变更:— v2版本中已移除,对非组件元素使用
createWrapper()构造函数 来源new DOMWrapper() -
破坏性变更:— v2版本中默认不再渲染存根组件的默认插槽内容 来源
shallowMount -
破坏性变更:— 现在仅支持
find()语法;使用querySelector来定位Vue组件 来源findComponent() -
破坏性变更:和
setSelected— v2版本中已移除,功能合并到setChecked中 来源setValue() -
破坏性变更:— v2版本中更名为
attachToDocument来源attachTo -
破坏性变更:— v2版本中已移除,使用
emittedByOrder替代 来源emitted() -
新增:— v2.3.0版本中添加,以支持SSR测试 来源
renderToString() -
新增:/
enableAutoUnmount()— v2版本中替代disableAutoUnmount()来源enableAutoDestroy -
已废弃:— v2版本中已移除并合并到
scopedSlots挂载选项中 来源slots
其他变更:和返回 · 作用域在字符串模板中以暴露 · 、、、、和已移除
setValue()trigger()nextTickslotsparamsisisEmptyisVueInstancenamesetMethodscontainsBest Practices
最佳实践
- Always methods that return
await(nextTick,trigger,setValue,setProps) to ensure DOM updates are processed before running assertions sourcesetData
ts
// Preferred
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('Count: 1')
// Avoid — assertion runs before DOM update
wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('Count: 1')-
Preferand
get()overgetComponent()andfind()when you expect the element to exist — they throw immediately if not found, providing clearer test failures sourcefindComponent() -
Useto resolve non-Vue asynchronous operations such as mocked API calls (axios) or external promise-based logic that Vue doesn't track source
flushPromises() -
Enablein your test setup to automatically clean up wrappers after every test, preventing state pollution and memory leaks source
enableAutoUnmount(afterEach)
ts
import { enableAutoUnmount } from '@vue/test-utils'
import { afterEach } from 'vitest'
enableAutoUnmount(afterEach)-
Wrap components within a
async setup()component within your test to correctly handle their asynchronous initialization source<Suspense> -
Enablewhen using
config.global.renderStubDefaultSlot = truemounting to ensure content within default slots is rendered for verification sourceshallow -
Preferwith specific
mount()overglobal.stubsto keep tests more production-like while still isolating specific complex child components sourceshallow: true -
Useto pass data to components using
global.provide, ensuring the component tree's dependency injection works as it does in production sourceinject -
Test complex composables by mounting a minimalthat calls the composable, allowing you to verify internal state via
TestComponentsourcewrapper.vm
ts
const TestComponent = defineComponent({
setup() {
return { ...useMyComposable() }
}
})
const wrapper = mount(TestComponent)
expect(wrapper.vm.someValue).toBe(true)- Stub custom directives using the naming convention (e.g.,
vName) in thevTooltip: truemounting option sourceglobal.stubs
- 对于返回的方法(
nextTick、trigger、setValue、setProps),始终使用setData,以确保在运行断言前处理完DOM更新 来源await
ts
// 推荐写法
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('Count: 1')
// 不推荐 — 断言在DOM更新前执行
wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('Count: 1')-
当你确定元素存在时,优先使用和
get()而非getComponent()和find()— 如果未找到元素,它们会立即抛出错误,提供更清晰的测试失败信息 来源findComponent() -
使用来解析Vue未跟踪的非Vue异步操作,例如模拟API调用(axios)或基于外部Promise的逻辑 来源
flushPromises() -
在测试设置中启用,以在每次测试后自动清理包装器,防止状态污染和内存泄漏 来源
enableAutoUnmount(afterEach)
ts
import { enableAutoUnmount } from '@vue/test-utils'
import { afterEach } from 'vitest'
enableAutoUnmount(afterEach)-
在测试中使用组件包裹带有
<Suspense>的组件,以正确处理其异步初始化 来源async setup() -
当使用挂载时,启用
shallow,以确保默认插槽内的内容被渲染以便验证 来源config.global.renderStubDefaultSlot = true -
优先使用带有特定的
global.stubs而非mount(),使测试更贴近生产环境,同时仍能隔离特定复杂子组件 来源shallow: true -
使用向使用
global.provide的组件传递数据,确保组件树的依赖注入与生产环境一致 来源inject -
通过挂载调用该组合式函数的最小来测试复杂的组合式函数,允许你通过
TestComponent验证内部状态 来源wrapper.vm
ts
const TestComponent = defineComponent({
setup() {
return { ...useMyComposable() }
}
})
const wrapper = mount(TestComponent)
expect(wrapper.vm.someValue).toBe(true)- 在挂载选项中使用
global.stubs命名约定(例如vName)来存根自定义指令 来源vTooltip: true