mcpField
V
vue-test-utils-skilld

vue-test-utils-skilld

github.com/skilld-devTesting & quality184167 installsUpdated 3w ago
Repository

About vue-test-utils-skilld

vue-test-utils-skilld documents `@vue/test-utils` for Vue 3 developers using runners such as Vitest or Jest to mount components and inspect behavior. It supports full and shallow mounting, server-side string rendering, wrapper queries, and async update utilities.

Key features

  1. 01Mount and shallow-mount components
  2. 02Query component and DOM wrappers
  3. 03Trigger events and set props
  4. 04Wait for promises and Vue updates
  5. 05Render components to server-side strings
  6. 06Auto-unmount through test hooks

Use cases

  1. 01Test component state changes after a click
  2. 02Verify props, form values, and emitted events
  3. 03Check whether optional elements exist
  4. 04Test interfaces that depend on async requests
  5. 05Write server-rendering tests for components

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/skilld-dev/vue-ecosystem-skills --skill vue-test-utils-skilld

After installing, type/vue-test-utils-skilldin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
migration-v2.md
mounting-options.md
recipes.md
wrapper-api.md

Details

From the project README

@vue/test-utils 2.5.1 (Vue 3)

Prepared source: input/source, version 2.5.1 (package.json:3). Requires Vue 3.x and @vue/compiler-dom 3.x as peers; @vue/server-renderer 3.x is an optional peer needed only for renderToString (package.json:73-81). Test-runner agnostic (Vitest, Jest, others). Needs a browser-like DOM environment (jsdom or happy-dom).

Install: npm install @vue/test-utils --save-dev

Quick start

import { mount } from '@vue/test-utils'
import Counter from './Counter.vue'

test('increments', async () => {
  const wrapper = mount(Counter, { props: { start: 1 } })
  await wrapper.find('button').trigger('click')
  expect(wrapper.text()).toContain('2')
})

Public API surface

Exports from dist/src/index.d.ts:13:

ExportKindUse
mountfunctionMount a component, returns VueWrapper (dist/src/mount.d.ts:17)
shallowMountfunctionSame as mount with all child components stubbed (dist/src/mount.d.ts:22)
renderToStringfunctionSSR-render a component to a string, Promise<string> (dist/src/renderToString.d.ts:4)
flushPromisesfunctionAwait pending non-Vue promises (mocked API calls, timers) (dist/src/utils/flushPromises.d.ts:1)
enableAutoUnmount(hook)functionUnmount all wrappers via a test hook (dist/src/utils/autoUnmount.d.ts:4)
disableAutoUnmountfunctionStop auto unmounting (dist/src/utils/autoUnmount.d.ts:3)
VueWrapperclassWrapper around a mounted component instance (dist/src/vueWrapper.d.ts:5)
DOMWrapperclassWrapper around a DOM element; new DOMWrapper(document.body) (dist/src/domWrapper.d.ts:4)
RouterLinkStubcomponentStub for <router-link> (dist/src/components/RouterLinkStub.d.ts:1)
configobjectShared default mount options and wrapper plugins (dist/src/config.d.ts:32)
createWrapperErrorfunctionInternal; produces the error wrapper returned by find misses

Core rules

  • await every method that returns a promise: trigger, setValue, setProps, setData, renderToString, flushPromises. Without await, assertions run before the DOM updates.
  • Use get() / getComponent() when the element must exist; they throw on miss. Use find() / findComponent() only when absence is a valid outcome; they return an error wrapper whose exists() is false.
  • find() accepts CSS selectors only. To locate a child component use findComponent(Component), findComponent({ name: 'Foo' }), findComponent({ ref: 'foo' }), or a CSS selector.
  • Register enableAutoUnmount(afterEach) once in test setup to prevent state leaks between tests.
  • Use flushPromises() for promises Vue does not track (mocked HTTP clients, setTimeout).
  • wrapper.vm only reliably exposes what the component exposes: options-API state, defineExpose() bindings (since 2.5.0, PR #2927), or setup() return values.
import { enableAutoUnmount } from '@vue/test-utils'
import { afterEach } from 'vitest'

enableAutoUnmount(afterEach)

Version notes: 2.4.10 -> 2.5.1

Earlier v2 milestones still relevant

Migrating from v1 (Vue 2)

Full table: migration reference, official guide https://test-utils.vuejs.org/migration/

  • propsData -> props; createLocalVue removed -> global.plugins / global.mixins
  • mocks, stubs, provide, directives moved under global
  • destroy() -> unmount(); findAll().at(i) -> findAll()[i] (returns an array)
  • createWrapper() removed -> new DOMWrapper(el)
  • setChecked / setSelected removed -> merged into setValue
  • find() no longer finds components by name; use findComponent
  • shallowMount no longer renders default slot content of stubs; restore with config.global.renderStubDefaultSlot = true
  • Removed: is, isEmpty, isVueInstance, name, setMethods, contains, scopedSlots (merged into slots)

Best practices

  • Prefer mount with targeted global.stubs over shallowMount. Shallow tests assert structure, not behavior, and stubbed children hide real interactions. https://test-utils.vuejs.org/guide/advanced/stubs-shallow-mount
  • If you do stub broadly, set config.global.renderStubDefaultSlot = true so default slot content of stubs still renders (dist/src/types.d.ts:125-131).
  • Stub directives with the vName key: global.stubs: { vTooltip: true } or pass a replacement directive object.
  • <transition> and <transition-group> are stubbed by default (dist/src/types.d.ts:120-124); custom transition stubs are supported.
  • Pass inject values through global.provide, matching production injection. For typed injection keys, wrap the key: provide: { [injectionKey as symbol]: value }.
  • Test composables by mounting a minimal host component and reading state from wrapper.vm.
  • Wrap components with async setup() in a <Suspense> host before mounting.
  • For <Teleport>, either stub it (global.stubs: { teleport: true }) or create the target element in beforeEach and query it with document.querySelector. https://test-utils.vuejs.org/guide/advanced/teleport
  • Use RouterLinkStub when testing around <router-link> without installing a router: global.stubs: { 'router-link': RouterLinkStub }.

References

Comments

Markdown supported