N Next.js 中文
原文 ↗

如何为 Next.js 配置 Vitest

了解如何为 Next.js 配置 Vitest 以进行单元测试。

更新于 2026-08-25Next.js 16.4.6

Vitest 和 React Testing Library 经常搭配使用来进行单元测试。本指南将向你展示如何为 Next.js 配置 Vitest 并编写你的第一个测试。

须知:

由于 async 服务器组件对 React 生态而言是全新的,Vitest 目前尚不支持它们。虽然你仍然可以对同步的服务器组件和客户端组件运行单元测试,但我们建议对 async 组件使用 E2E 测试。

快速开始

你可以使用 create-next-app 搭配 Next.js 的 with-vitest 示例来快速上手:

pnpm

pnpm create next-app --example with-vitest with-vitest-app

npm

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

yarn

yarn create next-app --example with-vitest with-vitest-app

bun

bun create next-app --example with-vitest with-vitest-app

手动配置

要手动配置 Vitest,请将 vitest 以及以下包安装为开发依赖:

pnpm

# Using TypeScript
pnpm add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths
# Using JavaScript
pnpm add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom

npm

# Using TypeScript
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths
# Using JavaScript
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom

yarn

# Using TypeScript
yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths
# Using JavaScript
yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom

bun

# Using TypeScript
bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths
# Using JavaScript
bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom

在项目根目录下创建一个 vitest.config.mts|js 文件,并添加以下配置项:

TypeScript

vitest.config.mtsts
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
  plugins: [tsconfigPaths(), react()],
  test: {
    environment: 'jsdom',
  },
})

JavaScript

vitest.config.jsjs
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  test: {
    environment: 'jsdom',
  },
})

有关配置 Vitest 的更多信息,请参阅 Vitest 配置 文档。

然后,在 package.json 中添加一个 test 脚本:

package.jsonjson
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "test": "vitest"
  }
}

当你运行 npm run test 时,Vitest 默认会监听项目中的变更。

创建你的第一个 Vitest 单元测试

通过创建一个测试来检查 <Page /> 组件是否成功渲染标题,以确认一切正常工作:

TypeScript

app/page.tsxtsx
import Link from 'next/link'

export default function Page() {
  return (
    <div>
      <h1>Home</h1>
      <Link href="/about">About</Link>
    </div>
  )
}
__tests__/page.test.tsxtsx
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'
import Page from '../app/page'

test('Page', () => {
  render(<Page />)
  expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined()
})

JavaScript

app/page.jsxjsx
import Link from 'next/link'

export default function Page() {
  return (
    <div>
      <h1>Home</h1>
      <Link href="/about">About</Link>
    </div>
  )
}
__tests__/page.test.jsxjsx
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'
import Page from '../app/page'

test('Page', () => {
  render(<Page />)
  expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined()
})

须知:

上面的示例使用了常见的 __tests__ 约定,但测试文件也可以与 app 路由放在同一目录下。

运行测试

然后,运行以下命令来运行你的测试:

pnpm

pnpm test

npm

npm run test

yarn

yarn test

bun

bun run test

其他资源

以下资源可能会对你有所帮助: