Skip to content

Lesson 8 of 9 · 26m

Testing Vue Applications: Components, Composables, and Stores

A pragmatic testing strategy with Vitest and Vue Test Utils — behavior over implementation, realistic store setup, and the async pitfalls that create flaky tests.

Vue tests fail for predictable reasons: they assert implementation details, ignore asynchronous rendering, or instantiate stores in ways production never does. Good tests answer one question: if behavior regresses, will this fail before users notice? Everything else is scaffolding.

Test behavior through the component boundary

import { mount } from "@vue/test-utils";
import UserSearch from "./UserSearch.vue";

it("shows an empty result state", async () => {
  fetchMock.mockResolvedValue({ users: [] });
  const wrapper = mount(UserSearch);

  await wrapper.get("input").setValue("nobody");
  await flushPromises();

  expect(wrapper.text()).toContain("No users found");
});

Use Vue Test Utils to mount, interact, await, and assert rendered output. Avoid testing internal refs unless there is no user-visible alternative. The more a test depends on implementation details, the more it punishes refactoring without catching regressions.

Composables need lifecycle context

Composables that use lifecycle hooks or effects often need a host component in tests:

function withSetup<T>(composable: () => T) {
  let result!: T;
  mount({
    setup() {
      result = composable();
      return () => null;
    },
  });
  return result;
}

Pure composables can be invoked directly. Stateful ones should be tested where cleanup and reactive updates actually happen. Test cleanup explicitly: listener removed, timer cancelled, observer disconnected. Those are real production behaviors, not implementation trivia.

Pinia tests need a fresh active pinia

import { createPinia, setActivePinia } from "pinia";

beforeEach(() => {
  setActivePinia(createPinia());
});

Never reuse one global store across tests unless you deliberately want order dependence. Seed state through public actions or direct state setup, assert externally visible getters/state, and mock network boundaries rather than fetch internals scattered through actions.

Async rendering must be awaited

setValue triggers input handling, but watchers, promises, and nextTick often resolve afterward. Use flushPromises, nextTick, or testing-library style queries depending on the stack. A test that passes only because everything happened to resolve in time is flaky in CI, where timing is adversarial.

What not to test

Do not test Vue itself: whether v-if hides an element, whether a computed recomputes, whether a prop is passed. Do test your branches: empty states, permission denial, error handling, keyboard interaction, pagination boundaries. Snapshot tests are a weak substitute for meaningfully named expectations.

Field notes

  • Router and store dependencies should be real enough to exercise integration but isolated enough to reset per test.
  • Accessibility assertions belong in component tests: labels, roles, focus movement, and keyboard activation.
  • Fake timers and real network mocks do not mix casually. Pick a deterministic clock strategy.
  • If a test needs ten lines of internal mocking, the component boundary is probably wrong; refactor before writing more tests.

Was this lesson helpful?

Sign in to rate