Next.js + Testing Library
Test like a user: render, screen, user-event and jest-dom. This page shows exactly what pickstack adds to a Next.js 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 next --modules testing-library --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
@testing-library/dom | ^10.4.2 | dev |
@testing-library/jest-dom | ^8.0.0 | dev |
@testing-library/react | ^16.3.3 | dev |
@testing-library/user-event | ^14.6.13 | dev |
@vitejs/plugin-react | ^6.3.0 | dev |
jsdom | ^31.0.1 | dev |
vitest | ^5.1.1 | dev |
New files
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import Counter from "./Counter";
describe("Counter", () => {
it("increments when +1 is clicked", async () => {
const user = userEvent.setup();
render(<Counter />);
await user.click(screen.getByRole("button", { name: "+1" }));
expect(screen.getByText("1")).toBeInTheDocument();
});
});
import react from "@vitejs/plugin-react";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [react()],
resolve: {
tsconfigPaths: true,
},
test: {
environment: "jsdom",
setupFiles: ["./vitest.setup.ts"],
},
});
import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/react";
import { afterEach } from "vitest";
afterEach(() => {
cleanup();
});
Changes to existing files
⋯ 2 unchanged lines <section className="page"> <h1 className="title">About</h1> <p className="text">+ Built with Next.js (App Router), React 19, TypeScript, Vitest, Testing+ Library.− Built with Next.js (App Router), React 19, TypeScript. </p> </section> );⋯ 2 unchanged lines
Project files
.gitignoreREADME.mdapp/about/page.tsxapp/globals.cssapp/layout.tsxapp/page.tsxcomponents/Counter.test.tsxcomponents/Counter.tsxnext.config.tspackage.jsontsconfig.jsonvitest.config.mtsvitest.setup.ts