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 --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
@nuxt/test-utils | ^4.4.0 | dev |
@testing-library/dom | ^10.4.2 | dev |
@testing-library/jest-dom | ^8.0.0 | dev |
@testing-library/user-event | ^14.6.13 | dev |
@testing-library/vue | ^8.1.0 | dev |
@vue/test-utils | ^2.6.0 | dev |
happy-dom | ^21.0.4 | dev |
vitest | ^5.1.1 | dev |
New files
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();
});
});
import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/vue";
import { afterEach } from "vitest";
afterEach(() => {
cleanup();
});
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
<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
.gitignoreREADME.mdapp/app.vueapp/assets/css/main.cssapp/components/CounterCard.vueapp/pages/about.vueapp/pages/index.vuenuxt.config.tspackage.jsonpublic/robots.txttest/nuxt/CounterCard.test.tstest/nuxt/setup.tstsconfig.jsonvitest.config.ts