How to Set Up Jest in Your Next.js Project: A Complete Guide

Why Jest with Next.js?

Jest is a popular testing framework that pairs well with React Testing Library to validate component behavior and UI rendering. While Jest doesn’t currently support async Server Components, it’s perfect for testing synchronous Server and Client Components. For async components, consider using end-to-end testing tools like Playwright or Cypress.


Quickstart: Scaffold with create-next-app

The fastest way to get started is by using the official example:

npx create-next-app@latest --example with-jest with-jest-app

This sets up a Next.js project pre-configured with Jest.

Manual Setup: Step-by-Step

If you prefer manual control, here’s how to configure Jest from scratch:

1. Install Dependencies

npm install -D jest jest-environment-jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom ts-node @types/jest

2. Initialize Jest Config

npm init jest@latest

This creates a jest.config.ts file. Update it to use next/jest:

import type { Config } from 'jest'
import nextJest from 'next/jest.js'

const createJestConfig = nextJest({ dir: './' })

const config: Config = {
  coverageProvider: 'v8',
  testEnvironment: 'jsdom',
}

export default createJestConfig(config)

Optional Enhancements

Absolute Imports & Module Aliases

If you're using path aliases in tsconfig.json, map them in Jest:

"paths": {
  "@/components/*": ["components/*"]
}
moduleNameMapper: {
  '^@/components/(.*)$': '<rootDir>/components/$1',
}

Custom Matchers

Enable matchers like .toBeInTheDocument() by adding this to your config:

setupFilesAfterEnv: ['<rootDir>/jest.setup.ts']

And in jest.setup.ts:

import '@testing-library/jest-dom'

Add Test Scripts

Update your package.json:

"scripts": {
  "test": "jest",
  "test:watch": "jest --watch"
}

Writing Your First Test

Create a __tests__ folder and add a simple test:

// __tests__/page.test.jsx
import '@testing-library/jest-dom'
import { render, screen } from '@testing-library/react'
import Page from '../app/page'

describe('Page', () => {
  it('renders a heading', () => {
    render(<Page />)
    const heading = screen.getByRole('heading', { level: 1 })
    expect(heading).toBeInTheDocument()
  })
})

Running Tests

Use your terminal:

npm run test

Setting up Jest in your Next.js project doesn’t have to be daunting. With built-in support and powerful testing libraries, you can ensure your components behave as expected—giving you confidence in every deploy.

Seng  Seang Leng

Seng Seang Leng

Web Developer

Author
I’m a passionate Full-Stack Developer with experience in both front-end and back-end development. My focus is on building modern, scalable, and user-friendly web applications.

ព័ត៌មានថ្មីៗ

តាមដានព័ត៌មានថ្មីៗ និងទស្សនៈជំនាញពីពិភពរបស់រូបិយប័ណ្ណឌីជីថល និងការអភិវឌ្ឍវេប។ ផ្នែកព័ត៌មានបច្ចេកវិទ្យារបស់យើងគ្របដណ្តប់លើនិន្នាការថ្មីៗ ការអភិវឌ្ឍសំខាន់ៗ និងការចែករំលែកចំណេះដឹងពីបច្ចេកវិទ្យាប្លុកខេន និងវេបសម័យទំនើប។
CNN3 hours ago
Justice Department fires top career prosecutor in Lindsey Halligan-led office
CNN3 hours ago
Sen. Mark Kelly files lawsuit alleging Hegseth violated his rights with push for punishment over illegal order video
Google News5 hours ago
Department of Homeland Security announces drone technology investments
Google News6 hours ago
Google Confirms Multiyear AI Deal to Power Apple Models, Siri
IGN37 minutes ago
Heated Rivalry Author Rachel Reid Announces New Book in the Series, Titled 'Unrivaled'
IGN48 minutes ago
New Gaming Tech, High End TV's & Mini PC's: Tech Trends That Will Define 2026 - CES Special Report

មើលបន្ថែមទៀត