vue-test-utils-skilld
Original:🇺🇸 English
Translated
ALWAYS use when writing code importing "@vue/test-utils". Consult for debugging, best practices, or modifying @vue/test-utils, vue/test-utils, vue test-utils, vue test utils, test-utils, test utils.
1installs
Added on
NPX Install
npx skill4agent add harlan-zw/vue-ecosystem-skills vue-test-utils-skilldTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →vuejs/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
API Changes
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()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