CoachnestCoachnest
Sign InGet Started
Back to course

Next.js 16: The Complete Developer Guide

…
—
Contents
1

What's New in Next.js 16

Reading14mFree
2

Installation, CLI & Your First Project

Reading12mFree
3

Project Structure & Conventions Deep Dive

Reading16m
4

Turbopack — The New Default Bundler

Video18m
5

Configuring TypeScript, ESLint & next.config.ts

Reading14m
6

Chapter 1 — Quiz

Quiz10m
7

App Router Fundamentals

Reading16m
8

Dynamic Routes, Catch-Alls & Type-Safe Params

Reading14m
9

Route Groups & Parallel Routes

Reading16m
10

Intercepting Routes & Modal Patterns

Reading12m
11

Loading, Error & Not-Found UI

Reading12m
12

Chapter 2 — Routing Quiz

Quiz12m
13

Understanding the Server/Client Boundary

Reading18m
14

Choosing When to use "use client"

Reading14m
15

Composing Server & Client Components

Reading14m
16

server-only, client-only & Code Splitting

Reading12m
17

Chapter 3 — Quiz

Quiz10m
18

Fetching Data in Server Components

Reading14m
19

The Next.js 16 Cache Model

Reading16m
20

Revalidation: revalidateTag, revalidatePath & On-Demand

Reading12m
21

Cache Components — Building Reusable Cached Functions

Reading14m
22

Search Params, Cookies & Dynamic APIs

Reading12m
23

Chapter 4 — Quiz

Quiz12m
24

Server Actions From First Principles

Reading16m
25

Forms with useActionState & Progressive Enhancement

Reading14m
26

Optimistic UI with useOptimistic

Reading12m
27

Validation with Zod + Server Actions

Reading12m
28

Chapter 5 — Quiz

Quiz10m
29

Static vs Dynamic vs Streaming

Reading14m
30

generateStaticParams & Pre-Rendering Dynamic Routes

Reading12m
31

Incremental Static Regeneration (ISR)

Reading12m
32

Partial Prerendering (PPR)

Reading16m
33

Edge Runtime vs Node.js Runtime

Reading12m
34

Rendering Strategies Deep Dive

Video22m
35

Chapter 6 — Quiz

Quiz12m
36

Tailwind CSS v4 with Next.js 16

Reading14m
37

CSS Modules, Global Styles & Scoped CSS

Reading10m
38

next/image — Smart, Fast Images

Reading14m
39

Fonts, Icons & Metadata

Reading12m
40

Chapter 7 — Quiz

Quiz10m
41

Middleware Fundamentals

Reading14m
42

Sessions, JWTs & Cookies

Reading16m
43

Protecting Server Components & Server Actions

Reading12m
44

Chapter 8 — Quiz

Quiz10m
45

Prisma with Next.js — The Production Setup

Reading14m
46

Mutations: Server Actions + Database Writes

Reading12m
47

Route Handlers & REST APIs

Reading12m
48

Chapter 9 — Quiz

Quiz10m

Unit & Component Testing with Vitest

Reading12m
50

End-to-End Testing with Playwright

Reading14m
51

Deploying to Vercel

Reading12m
52

Self-Hosting with Docker

Reading14m
53

Production Performance Checklist

Reading12m
54

Final Assessment — Next.js 16 Mastery

Quiz20m
←→navigate lessons
Chapter 10 of 10·Chapter 10 — Testing, Deployment & Production
Lesson 49 of 54Reading12 min

Unit & Component Testing with Vitest

#Unit & Component Testing with Vitest¶

Vitest is the modern test runner for Next.js — Jest-compatible API, fast (Vite-powered), and works out of the box with TypeScript and React.

Install¶

bash
1 line
1npm i -D vitest @testing-library/react @testing-library/jest-dom @vitejs/plugin-react jsdom

vitest.config.ts¶

ts
15 lines
1import { defineConfig } from "vitest/config";
2import react from "@vitejs/plugin-react";
3import path from "node:path";
4
5export default defineConfig({
6  plugins: [react()],
7  test: {
8    environment: "jsdom",
9    globals: true,
10    setupFiles: ["./vitest.setup.ts"],
11  },
12  resolve: {
13    alias: { "@": path.resolve(__dirname, "./") },
14  },
15});
ts
2 lines
1// vitest.setup.ts
2import "@testing-library/jest-dom/vitest";

A Component Test¶

tsx
11 lines
1// components/CourseCard.test.tsx
2import { render, screen } from "@testing-library/react";
3import { CourseCard } from "./CourseCard";
4
5const course = { id: "1", title: "Next.js 16", slug: "next-16", price: 2999 };
6
7test("renders title and price", () => {
8  render(<CourseCard course={course} />);
9  expect(screen.getByRole("heading", { name: /next\.js 16/i })).toBeInTheDocument();
10  expect(screen.getByText("₹2,999")).toBeInTheDocument();
11});

Testing a Server Action¶

Server Actions are just async functions. Test them like any other:

ts
20 lines
1// app/courses/actions.test.ts
2import { createCourse } from "./actions";
3import { db } from "@/lib/db";
4
5vi.mock("@/lib/db", () => ({
6  db: { course: { create: vi.fn() } },
7}));
8
9vi.mock("@/lib/auth-guard", () => ({
10  requireUser: vi.fn().mockResolvedValue({ id: "u1", role: "USER" }),
11}));
12
13test("creates a course with valid input", async () => {
14  (db.course.create as any).mockResolvedValue({ id: "c1", slug: "abc" });
15  const fd = new FormData();
16  fd.set("title", "My course");
17  fd.set("description", "A long enough description for validation.");
18  const result = await createCourse(null, fd);
19  expect(db.course.create).toHaveBeenCalled();
20});

Testing Async Server Components¶

Server Components are async functions returning JSX. Test them directly:

tsx
7 lines
1import Page from "./page";
2
3test("renders 5 courses", async () => {
4  const el = await Page({ params: Promise.resolve({}) });
5  render(el);
6  expect(screen.getAllByRole("article")).toHaveLength(5);
7});

What to Test¶

  • Server Actions — input validation, auth checks, happy path, error paths
  • Pure utility functions — slugify, format, validation schemas
  • Critical components — buttons, forms, complex layouts with state
  • Permission helpers — can.editCourse, role gates

What not to test in unit tests:

  • Real database queries (use integration / E2E)
  • Authentication flow end-to-end (use Playwright)
  • Visual styling (use visual regression tools like Chromatic)

CI Script¶

json
7 lines
1{
2  "scripts": {
3    "test": "vitest",
4    "test:run": "vitest run",
5    "test:coverage": "vitest run --coverage"
6  }
7}

Add to your CI: npm run test:run.

Previous

Chapter 9 — Quiz

Next

End-to-End Testing with Playwright

Use ← → arrow keys to navigate between lessons