To set up Jest, install jest , @testing-library/react, @testing-library/jest-dom:

npm install --save-dev jest @testing-library/react @testing-library/jest-dom
Create a jest.config.js file in your project's root directory and add the following:

// jest.config.js
const nextJest = require('next/jest')

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './',
})

// Add any custom config to be passed to Jest
const customJestConfig = {
  // Add more setup options before each test is run
  // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
  moduleDirectories: ['node_modules', '<rootDir>/'],
  testEnvironment: 'jest-environment-jsdom',
}

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)
To set up Jest, install jest , @testing-library/react, @testing-library/jest-dom:

npm install --save-dev jest @testing-library/react @testing-library/jest-dom
Create a jest.config.js file in your project's root directory and add the following:

// jest.config.js
const nextJest = require('next/jest')

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './',
})

// Add any custom config to be passed to Jest
const customJestConfig = {
  // Add more setup options before each test is run
  // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
  moduleDirectories: ['node_modules', '<rootDir>/'],
  testEnvironment: 'jest-environment-jsdom',
}

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)
@testing-library/jest-dom includes a set of convenient custom matchers such as .toBeInTheDocument() making it easier to write tests. You can import the custom matchers for every test by adding the following option to the Jest configuration file:

// jest.config.js
setupFilesAfterEnv: ['<rootDir>/jest.setup.js']
Here are the recommended options to configure Jest for Next.js:

// jest.config.js
module.exports = {
  collectCoverageFrom: [
    '**/*.{js,jsx,ts,tsx}',
    '!**/*.d.ts',
    '!**/node_modules/**',
  ],
  moduleNameMapper: {
    // Handle CSS imports (with CSS modules)
    // https://jestjs.io/docs/webpack#mocking-css-modules
    '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',

    // Handle CSS imports (without CSS modules)
    '^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',

    // Handle image imports
    // https://jestjs.io/docs/webpack#handling-static-assets
    '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i': `<rootDir>/__mocks__/fileMock.js`,

    // Handle module aliases
    '^@/components/(.*)$': '<rootDir>/components/$1',
  },
  // Add more setup options before each test is run
  // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
  testEnvironment: 'jsdom',
  transform: {
    // Use babel-jest to transpile tests with the next/babel preset
    // https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
    '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
  },
  transformIgnorePatterns: [
    '/node_modules/',
    '^.+\\.module\\.(css|sass|scss)$',
  ],
}
Then, inside jest.setup.js, add the following import:

// jest.setup.js
import '@testing-library/jest-dom/extend-expect'
If your project is using Module Path Aliases, you will need to configure Jest to resolve the imports by matching the paths option in the jsconfig.json file with the moduleNameMapper option in the jest.config.js file. For example:

// tsconfig.json or jsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/components/*": ["components/*"]
    }
  }
}
Stylesheets and images aren't used in the tests but importing them may cause errors, so they will need to be mocked. Create the mock files referenced in the configuration above - fileMock.js and styleMock.js - inside a __mocks__ directory:

// __mocks__/fileMock.js
module.exports = {
  src: '/img.jpg',
  height: 24,
  width: 24,
  blurDataURL: 'data:image/png;base64,imagedata',
}
You can use create-next-app with the with-jest example to quickly get started with Jest and React Testing Library:

npx create-next-app@latest --example with-jest with-jest-app
Add the Jest executable in watch mode to the package.json scripts:

"scripts": {
  "dev": "next dev",
  "build": "next build",
  "start": "next start",
  "test": "jest --watch"
}
For example, we can add a test to check if the component successfully renders a heading:

// __tests__/index.test.jsx

import { render, screen } from '@testing-library/react'
import Home from '../pages/index'

describe('Home', () => {
  it('renders a heading', () => {
    render(<Home />)

    const heading = screen.getByRole('heading', {
      name: /welcome to next\.js!/i,
    })

    expect(heading).toBeInTheDocument()
  })
})

Recommend

Nextjs Testing Jest and React Testing Library Quickstart

Nextjs Testing Playwright Creating your first Playwright end-to-end test

Nextjs Testing Playwright Manual setup

Nextjs Testing Playwright Quickstart

Nextjs Testing Cypress Getting ready for Continuous Integration (CI)

Nextjs Testing Cypress Creating your first Cypress integration test

Nextjs Testing Cypress Manual setup

Nextjs Testing Cypress Quickstart

Nextjs Middleware Execution Order

Nextjs Middleware API

Nextjs Middleware Usage

Matplotlib Overview of axisartist toolkit FloatingAxis

Matplotlib Overview of axisartist toolkit GridHelper

Matplotlib Overview of axisartist toolkit Rotation and Alignment of TickLabels Adjusting pad

Matplotlib Overview of axisartist toolkit Rotation and Alignment of TickLabels

Matplotlib Overview of axisartist toolkit HowTo

Matplotlib Overview of axisartist toolkit Default AxisArtists

Matplotlib Overview of axisartist toolkit axisartist axisartist with ParasiteAxes

Matplotlib Overview of axisartist toolkit axisartist

Matplotlib The mplot3d Toolkit Tri-Surface plots

Matplotlib The mplot3d Toolkit

Matplotlib Overview of mpl_toolkits.axes_grid1 AxesDivider

Matplotlib Overview of mpl_toolkits.axes_grid1 axes_grid1 RGBAxes

Matplotlib Overview of mpl_toolkits.axes_grid1 axes_grid1 InsetLocator

Matplotlib Overview of mpl_toolkits.axes_grid1 axes_grid1 ParasiteAxes Example 2. twin

Matplotlib Overview of mpl_toolkits.axes_grid1 axes_grid1 colorbar whose height (or width) in sync with the master axes scatter_hist.py with AxesDivider

Matplotlib Overview of mpl_toolkits.axes_grid1 axes_grid1 AxesDivider Class

Matplotlib Image tutorial Plotting numpy arrays as images Array Interpolation schemes

Matplotlib Image tutorial Plotting numpy arrays as images Examining a specific data range

Matplotlib Image tutorial Plotting numpy arrays as images Color scale reference

Matplotlib Image tutorial Plotting numpy arrays as images Applying pseudocolor schemes to image plots

Matplotlib Image tutorial Plotting numpy arrays as images

Matplotlib Image tutorial Importing image data into Numpy arrays

Matplotlib Image tutorial Startup commands

Matplotlib Pyplot tutorial Logarithmic and other nonlinear axes

Matplotlib Pyplot tutorial Working with text Annotating text

Matplotlib Pyplot tutorial Working with text Using mathematical expressions in text

Matplotlib Pyplot tutorial Working with text

Matplotlib Pyplot tutorial Working with multiple figures and axes

Matplotlib Pyplot tutorial Controlling line properties

Matplotlib Pyplot tutorial Plotting with categorical variables

Matplotlib Pyplot tutorial Plotting with keyword strings

Matplotlib Pyplot tutorial Intro to pyplot Formatting the style of your plot

Matplotlib Pyplot tutorial Intro to pyplot

Matplotlib Basic Usage Working with multiple Figures and Axes

Matplotlib Basic Usage Color mapped data

Matplotlib Basic Usage Axis scales and ticks Additional Axis objects

Matplotlib Basic Usage Axis scales and ticks Plotting dates and strings