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.

News - technology

Stay informed with the latest updates and expert insights from the world of cryptocurrency and web development. Our technology news section covers emerging trends, key developments, and thought leadership in blockchain and modern web technologies.
Technology5 hours ago
Porsche’s insanely clever hybrid engine comes to the 911 Turbo S
Technology5 hours ago
Porsche’s Most Powerful 911 Ever Is a Hybrid Sports Car
Bing newsSaturday 06 Sep 2025
8 Finished Sci-Fi Shows on Netflix Everyone Should Binge
Bing newsSaturday 06 Sep 2025
8 Finished Sci-Fi Shows on Netflix Everyone Should Binge
cnn.com1 hours ago
‘The Conjuring: Last Rites’ jolts box office with third-highest opening for a horror movie
cnn.com4 hours ago
US to issue ‘a refund on about half of the tariffs’ if SCOTUS rules Trump overstepped presidential powers, says Bessent
Economic1 hours ago
U.S. plans aggressive economic war against Putin's regime
Economic18 hours ago
Just a moment...
Developer TechFriday 05 Sep 2025
PAM unifies IT and OT security to protect vital IoT networks
Developer TechFriday 05 Sep 2025
UK AI sector growth hits record £2.9B investment
Green Tech6 hours ago
Opinion | Why it makes sense for green-tech exporter China to ramp up coal power
Green Tech14 hours ago
Scientists achieve historic feat while working with old magnets: 'A significant step'

see more