pickstack

Nuxt + Testing Library

Test like a user: render, screen, user-event and jest-dom. This page shows exactly what pickstack adds to a Nuxt 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 nuxt --modules testing-library --yes
Customize in the configurator

Packages it adds

PackageVersionType
@nuxt/test-utils^4.4.0dev
@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
@vue/test-utils^2.6.0dev
happy-dom^21.0.4dev
vitest^5.1.1dev

New files

test/nuxt/CounterCard.test.ts
import { renderSuspended } from "@nuxt/test-utils/runtime";
import userEvent from "@testing-library/user-event";
import { screen } from "@testing-library/vue";
import { describe, expect, it } from "vitest";
import CounterCard from "~/components/CounterCard.vue";

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

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

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

afterEach(() => {
  cleanup();
});
vitest.config.ts
import { defineVitestProject } from "@nuxt/test-utils/config";
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    projects: [
      await defineVitestProject({
        test: {
          name: "nuxt",
          include: ["test/nuxt/*.{test,spec}.ts"],
          environment: "nuxt",
          setupFiles: ["./test/nuxt/setup.ts"],
        },
      }),
    ],
  },
});

Changes to existing files

app/pages/about.vue (changed)
 <template>   <section class="page">     <h1 class="title">About</h1>+    <p class="text">+      Built with Nuxt 4, Vue 3, TypeScript, Vitest, Testing Library.+    </p>−    <p class="text">Built with Nuxt 4, Vue 3, TypeScript.</p>   </section> </template>  

Project files

Related templates