unit-test-vue-pinia by github/awesome-copilot
npx skills add https://github.com/github/awesome-copilot --skill unit-test-vue-pinia使用此技能来创建或审查 Vue 组件、组合式函数和 Pinia 仓库的单元测试。保持测试小巧、确定性和行为优先。
wrapper.vm,即当没有合理的 DOM、prop、事件触发或仓库级断言时。beforeEach() 中进行显式设置,并在每个测试中重置模拟。references/pinia-patterns.md 中的已检入参考资料作为标准 Pinia 测试设置的本地事实来源。首先使用 references/pinia-patterns.md,当已检入的示例未覆盖该情况时,再参考 Pinia 的测试指南。
在挂载时使用 createTestingPinia 作为全局插件。默认优先使用 以确保一致性和更轻松的动作间谍断言。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
createSpy: vi.fnconst wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
}),
],
},
});
默认情况下,动作会被存根化和监视。当测试只需要验证动作是否被调用(或未被调用)时,使用 stubActions: true(默认值)。
以下设置也是有效的,不应被标记为不正确:
createTestingPinia({})。createSpy 的 createTestingPinia({ initialState: ... }) 或 createTestingPinia({ stubActions: ... })。setActivePinia(createTestingPinia(...))。当动作间谍断言是测试意图的一部分时,使用 createSpy: vi.fn。
仅当测试必须验证动作的真实行为和副作用时,才使用 stubActions: false。不要为了简单的"是否被调用"断言而默认开启它。
const wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
stubActions: false,
}),
],
},
});
initialState 为仓库状态提供种子const wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
initialState: {
counter: { n: 20 },
user: { name: "Leia Organa" },
},
}),
],
},
});
createTestingPinia 添加 Pinia 插件const wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
plugins: [myPiniaPlugin],
}),
],
},
});
const pinia = createTestingPinia({ createSpy: vi.fn });
const store = useCounterStore(pinia);
store.double = 999;
// @ts-expect-error test-only reset of overridden getter
store.double = undefined;
当目标是验证仓库状态转换和动作行为而无需组件渲染时,优先使用 createPinia() 进行纯仓库测试。仅当需要存根化依赖的仓库、种子化的测试替身或动作间谍时,才使用 createTestingPinia()。
beforeEach(() => {
setActivePinia(createPinia());
});
it("increments", () => {
const counter = useCounterStore();
counter.increment();
expect(counter.n).toBe(1);
});
遵循 Vue Test Utils 指南:https://test-utils.vuejs.org/guide/
findComponent(...).vm.$emit(...),而不是接触父组件内部。nextTick。wrapper.emitted(...) 断言触发的事件及其载荷。wrapper.vm。将其视为例外,并保持断言范围狭窄。触发事件并断言载荷:
await wrapper.find("button").trigger("click");
expect(wrapper.emitted("submit")?.[0]?.[0]).toBe("Mango Mission");
更新输入并断言输出:
await wrapper.find("input").setValue("Agent Violet");
await wrapper.find("form").trigger("submit");
expect(wrapper.emitted("save")?.[0]?.[0]).toBe("Agent Violet");
create 或 update,返回完成的测试代码以及描述所选 Pinia 策略的简短说明。review,首先返回具体的发现,然后是缺失的覆盖率或脆弱性风险。references/pinia-patterns.md每周安装数
316
代码仓库
GitHub 星标数
26.7K
首次出现
8 天前
安全审计
安装于
gemini-cli287
codex285
opencode277
cursor275
github-copilot274
cline273
Use this skill to create or review unit tests for Vue components, composables, and Pinia stores. Keep tests small, deterministic, and behavior-first.
wrapper.vm only in exceptional cases when there is no reasonable DOM, prop, emit, or store-level assertion.beforeEach() and reset mocks every test.references/pinia-patterns.md as the local source of truth for standard Pinia test setups.Use references/pinia-patterns.md first, then fall back to Pinia's testing cookbook when the checked-in examples do not cover the case.
Use createTestingPinia as a global plugin while mounting. Prefer createSpy: vi.fn as the default for consistency and easier action-spy assertions.
const wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
}),
],
},
});
By default, actions are stubbed and spied. Use stubActions: true (default) when the test only needs to verify whether an action was called (or not called).
The following are also valid and should not be flagged as incorrect:
createTestingPinia({}) when the test does not assert Pinia action spy behavior.createTestingPinia({ initialState: ... }) or createTestingPinia({ stubActions: ... }) without createSpy, when the test only needs state seeding or action stubbing behavior and does not inspect generated spies.setActivePinia(createTestingPinia(...)) in store/composable-focused tests (without mounting a component) when mocking/seeding dependent stores is needed.Use createSpy: vi.fn when action spy assertions are part of the test intent.
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.
const wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
stubActions: false,
}),
],
},
});
initialStateconst wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
initialState: {
counter: { n: 20 },
user: { name: "Leia Organa" },
},
}),
],
},
});
createTestingPiniaconst wrapper = mount(ComponentUnderTest, {
global: {
plugins: [
createTestingPinia({
createSpy: vi.fn,
plugins: [myPiniaPlugin],
}),
],
},
});
const pinia = createTestingPinia({ createSpy: vi.fn });
const store = useCounterStore(pinia);
store.double = 999;
// @ts-expect-error test-only reset of overridden getter
store.double = undefined;
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.
beforeEach(() => {
setActivePinia(createPinia());
});
it("increments", () => {
const counter = useCounterStore();
counter.increment();
expect(counter.n).toBe(1);
});
Follow Vue Test Utils guidance: https://test-utils.vuejs.org/guide/
findComponent(...).vm.$emit(...) for child stub events instead of touching parent internals.nextTick only when updates are async.wrapper.emitted(...).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.Emit and assert payload:
await wrapper.find("button").trigger("click");
expect(wrapper.emitted("submit")?.[0]?.[0]).toBe("Mango Mission");
Update input and assert output:
await wrapper.find("input").setValue("Agent Violet");
await wrapper.find("form").trigger("submit");
expect(wrapper.emitted("save")?.[0]?.[0]).toBe("Agent Violet");
create or update, return the finished test code plus a short note describing the selected Pinia strategy.review, return concrete findings first, then missing coverage or brittleness risks.references/pinia-patterns.mdWeekly Installs
316
Repository
GitHub Stars
26.7K
First Seen
8 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli287
codex285
opencode277
cursor275
github-copilot274
cline273
Vue.js测试最佳实践:Vue 3组件、组合式函数、Pinia与异步测试完整指南
3,700 周安装