Unit Tests for Vue and Pinia

Unit Tests for Vue and Pinia

Guides an agent to write and review behaviour-focused unit tests for Vue 3 components, composables and Pinia stores using Vitest and Vue Test Utils.

Skill files2 files

references/

SKILL.md199 lines
1
---
2
name: unit-test-vue-pinia
3
category: testing
4
description: 'Write and review unit tests for Vue 3 + TypeScript + Vitest + Pinia codebases. Use when creating or updating tests for components, composables, and stores; mocking Pinia with createTestingPinia; applying Vue Test Utils patterns; and enforcing black-box assertions over implementation details.'
5
---
6
7
# unit-test-vue-pinia
8
9
Use this skill to create or review unit tests for Vue components, composables, and Pinia stores. Keep tests small, deterministic, and behavior-first.
10
11
## Workflow
12
13
1. Identify the behavior boundary first: component UI behavior, composable behavior, or store behavior.
14
2. Choose the narrowest test style that can prove that behavior.
15
3. Set up Pinia with the least powerful option that still covers the scenario.
16
4. Drive the test through public inputs such as props, form updates, button clicks, emitted child events, and store APIs.
17
5. Assert observable outputs and side effects before considering any instance-level assertion.
18
6. Return or review tests with clear behavior-oriented names and note any remaining coverage gaps.
19
20
## Core Rules
21
22
- Test one behavior per test.
23
- Assert observable input/output behavior first (rendered text, emitted events, callback calls, store state changes).
24
- Avoid implementation-coupled assertions.
25
- Access `wrapper.vm` only in exceptional cases when there is no reasonable DOM, prop, emit, or store-level assertion.
26
- Prefer explicit setup in `beforeEach()` and reset mocks every test.
27
- Use checked-in reference material in `references/pinia-patterns.md` as the local source of truth for standard Pinia test setups.
28
29
## Pinia Testing Approach
30
31
Use `references/pinia-patterns.md` first, then fall back to Pinia's testing cookbook when the checked-in examples do not cover the case.
32
33
### Default pattern for component tests
34
35
Use `createTestingPinia` as a global plugin while mounting.
36
Prefer `createSpy: vi.fn` as the default for consistency and easier action-spy assertions.
37
38
```ts
39
const wrapper = mount(ComponentUnderTest, {
40
global: {
41
plugins: [
42
createTestingPinia({
43
createSpy: vi.fn,
44
}),
45
],
46
},
47
});
48
```
49
50
By default, actions are stubbed and spied.
51
Use `stubActions: true` (default) when the test only needs to verify whether an action was called (or not called).
52
53
### Accepted minimal Pinia setups
54
55
The following are also valid and should not be flagged as incorrect:
56
57
- `createTestingPinia({})` when the test does not assert Pinia action spy behavior.
58
- `createTestingPinia({ initialState: ... })` or `createTestingPinia({ stubActions: ... })` without `createSpy`, when the test only needs state seeding or action stubbing behavior and does not inspect generated spies.
59
- `setActivePinia(createTestingPinia(...))` in store/composable-focused tests (without mounting a component) when mocking/seeding dependent stores is needed.
60
61
Use `createSpy: vi.fn` when action spy assertions are part of the test intent.
62
63
### Execute real actions only when needed
64
65
Use `stubActions: false` only when the test must validate the action's real behavior and side effects. Do not switch it on by default for simple "was called" assertions.
66
67
```ts
68
const wrapper = mount(ComponentUnderTest, {
69
global: {
70
plugins: [
71
createTestingPinia({
72
createSpy: vi.fn,
73
stubActions: false,
74
}),
75
],
76
},
77
});
78
```
79
80
### Seed store state with `initialState`
81
82
```ts
83
const wrapper = mount(ComponentUnderTest, {
84
global: {
85
plugins: [
86
createTestingPinia({
87
createSpy: vi.fn,
88
initialState: {
89
counter: { n: 20 },
90
user: { name: "Leia Organa" },
91
},
92
}),
93
],
94
},
95
});
96
```
97
98
### Add Pinia plugins through `createTestingPinia`
99
100
```ts
101
const wrapper = mount(ComponentUnderTest, {
102
global: {
103
plugins: [
104
createTestingPinia({
105
createSpy: vi.fn,
106
plugins: [myPiniaPlugin],
107
}),
108
],
109
},
110
});
111
```
112
113
### Getter override pattern for edge cases
114
115
```ts
116
const pinia = createTestingPinia({ createSpy: vi.fn });
117
const store = useCounterStore(pinia);
118
119
store.double = 999;
120
// @ts-expect-error test-only reset of overridden getter
121
store.double = undefined;
122
```
123
124
### Pure store unit tests
125
126
Prefer pure store tests with `createPinia()` when the goal is to validate store state transitions and action behavior without component rendering. Use `createTestingPinia()` only when you need stubbed dependent stores, seeded test doubles, or action spies.
127
128
```ts
129
beforeEach(() => {
130
setActivePinia(createPinia());
131
});
132
133
it("increments", () => {
134
const counter = useCounterStore();
135
counter.increment();
136
expect(counter.n).toBe(1);
137
});
138
```
139
140
## Vue Test Utils Approach
141
142
Follow Vue Test Utils guidance: <https://test-utils.vuejs.org/guide/>
143
144
- Mount shallow by default for focused unit tests.
145
- Mount full component trees only when integration behavior is the subject.
146
- Drive behavior through props, user-like interactions, and emitted events.
147
- Prefer `findComponent(...).vm.$emit(...)` for child stub events instead of touching parent internals.
148
- Use `nextTick` only when updates are async.
149
- Assert emitted events and payloads with `wrapper.emitted(...)`.
150
- Access `wrapper.vm` only when no DOM assertion, emitted event assertion, prop assertion, or store-level assertion can express the behavior. Treat it as an exception and keep the assertion narrowly scoped.
151
152
## Key Testing Snippets
153
154
Emit and assert payload:
155
156
```ts
157
await wrapper.find("button").trigger("click");
158
expect(wrapper.emitted("submit")?.[0]?.[0]).toBe("Mango Mission");
159
```
160
161
Update input and assert output:
162
163
```ts
164
await wrapper.find("input").setValue("Agent Violet");
165
await wrapper.find("form").trigger("submit");
166
expect(wrapper.emitted("save")?.[0]?.[0]).toBe("Agent Violet");
167
```
168
169
## Test Writing Workflow
170
171
1. Identify the behavior boundary to test.
172
2. Build minimal fixture data (only fields needed by that behavior).
173
3. Configure Pinia and required test doubles.
174
4. Trigger behavior through public inputs.
175
5. Assert public outputs and side effects.
176
6. Refactor test names to describe behavior, not implementation.
177
178
## Constraints and Safety
179
180
- Do not test private/internal implementation details.
181
- Do not overuse snapshots for dynamic UI behavior.
182
- Do not assert every field in large objects if only one behavior matters.
183
- Keep fake data deterministic; avoid random values.
184
- Do not claim a Pinia setup is wrong when it is one of the accepted minimal setups above.
185
- Do not rewrite working tests toward deeper mounting or real actions unless the behavior under test requires that extra surface area.
186
- Flag missing test coverage, brittle selectors, and implementation-coupled assertions explicitly during review.
187
188
## Output Contract
189
190
- For `create` or `update`, return the finished test code plus a short note describing the selected Pinia strategy.
191
- For `review`, return concrete findings first, then missing coverage or brittleness risks.
192
- When the safest choice is ambiguous, state the assumption that drove the chosen test setup.
193
194
## References
195
196
- `references/pinia-patterns.md`
197
- Pinia testing cookbook: <https://pinia.vuejs.org/cookbook/testing.html>
198
- Vue Test Utils guide: <https://test-utils.vuejs.org/guide/>
199

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 13,266 prompts

More by @OpenSourceBuffGuy

See all
Flower Petal Sunglasses Editorial Portrait prompt image

Flower Petal Sunglasses Editorial Portrait

Draws a low-angle high-fashion portrait of a platinum blonde woman in petal sunglasses and a petal choker against golden-hour terracotta walls, with camera and Midjourney settings.

image-prompt, gpt-image, midjourney, prompt-weights, negative-prompt, portrait, woman, close-up, low-angle, photorealistic, editorial, fashion-shoot, fashion, jewelry, golden-hour, natural-light, bokeh, vibrant-colors, dramatic-lighting, vertical-format, flowers, for-photographers, for-artists, for-designers, long-form, advanced

Real-World Asset Tokenization Platform prompt image

Real-World Asset Tokenization Platform

Acts as a security token platform architect that designs a compliant real-world asset tokenization platform with smart contracts, KYC, secondary markets and a phased plan.

system-prompt, act-as, blockchain, compliance, software-architecture, due-diligence, roadmap, real-estate, investing, for-founders, for-developers, technical-spec, structured-output, xml-tags, expert-advice, payments, risk-management, legal, long-form, advanced, planner, crypto, solidity

Surreal Ingredient Landscape Product Ad prompt image

Surreal Ingredient Landscape Product Ad

Creates a surreal premium ad illustration of an oil bottle set in a giant landscape made of its key ingredient, with waterfalls and tiny figures for scale.

image-prompt, gpt-image, placeholders, surreal, fantasy-world, advertising, product-photography, packaging-design, poster, landscape, cinematic, vertical-format, painting, for-marketers, for-designers, branding, quick, dreamy, forced-perspective

Reviews

Loading reviews…

Discussion

Loading comments…