pickstack

Vue + Testing Library

Test like a user: render, screen, user-event and jest-dom. This page shows exactly what pickstack adds to a Vue project for Testing Library: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.

$ npm create pickstack@latest my-app -- --framework vue --modules testing-library --yes
Customize in the configurator

Packages it adds

PackageVersionType
@testing-library/dom^10.4.2dev
@testing-library/jest-dom^8.0.0dev
@testing-library/user-event^14.6.13dev
@testing-library/vue^8.1.0dev
@types/jsdom^30.0.0dev
@vue/test-utils^2.6.0dev
jsdom^31.0.1dev
vitest^5.1.1dev

New files

src/components/__tests__/CounterCard.spec.ts
import userEvent from "@testing-library/user-event";
import { render, screen } from "@testing-library/vue";
import { describe, expect, it } from "vitest";
import CounterCard from "../CounterCard.vue";

describe("CounterCard", () => {
  it("increments when +1 is clicked", async () => {
    const user = userEvent.setup();
    render(CounterCard);

    await user.click(screen.getByRole("button", { name: "+1" }));

    expect(screen.getByText("1")).toBeInTheDocument();
  });
});
src/test-setup.ts
import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/vue";
import { afterEach } from "vitest";

afterEach(() => {
  cleanup();
});
tsconfig.vitest.json
{
  "extends": "./tsconfig.app.json",
  "include": ["src/**/__tests__/*", "src/test-setup.ts", "env.d.ts"],
  "exclude": [],
  "compilerOptions": {
    "lib": [],
    "types": ["node", "jsdom"],
    "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.vitest.tsbuildinfo"
  }
}
vitest.config.ts
import { fileURLToPath } from "node:url";
import { defineConfig, mergeConfig } from "vitest/config";
import viteConfig from "./vite.config.ts";

export default mergeConfig(
  viteConfig,
  defineConfig({
    test: {
      environment: "jsdom",
      setupFiles: ["./src/test-setup.ts"],
      root: fileURLToPath(new URL("./", import.meta.url)),
    },
  }),
);

Changes to existing files

tsconfig.app.json (changed)
 {   "extends": "@vue/tsconfig/tsconfig.dom.json",   "include": ["env.d.ts", "src/**/*", "src/**/*.vue"],+  "exclude": ["src/**/__tests__/*", "src/test-setup.ts"],−  "exclude": ["src/**/__tests__/*"],   "compilerOptions": {     "noUncheckedIndexedAccess": true,     "paths": {⋯ 6 unchanged lines
tsconfig.json (changed)
⋯ 5 unchanged lines     },     {       "path": "./tsconfig.app.json"+    },+    {+      "path": "./tsconfig.vitest.json"     }   ] }⋯ 1 unchanged lines

Project files

Related templates