NNext.js 中文网 GitHub

如何在 Next.js 中设置 Vitest

了解如何使用 Vitest 和 React Testing Library 配置 Next.js —— 两个流行的单元测试库。

Vitest 和 React Testing Library 经常一起用于单元测试。本指南将向你展示如何使用 Next.js 设置 Vitest,并编写你的第一个测试。

小知识: 由于 async Server Component 在 React 生态系统中是新增特性,Vitest 目前不支持它们。虽然你仍然可以为同步的 Server Component 和 Client Component 运行单元测试,但我们建议对 async 组件使用 E2E 测试。

快速开始#

你可以将 create-next-app 与 Next.js 的 with-vitest 示例结合使用,以快速上手:

bash · pnpm
pnpm create next-app --example with-vitest with-vitest-app
bash · npm
npx create-next-app@latest --example with-vitest with-vitest-app
bash · yarn
yarn create next-app --example with-vitest with-vitest-app
bash · bun
bun create next-app --example with-vitest with-vitest-app

手动设置#

Vitest 5 和 jsdom 31 需要比 Next.js 最低要求更新的 Node.js 版本。对于下面的设置,请使用 Node.js 24 版本线中的 24.15 或更高版本。如果你使用其他 Node.js 版本,请根据 Vitest 安装要求和 jsdom 要求选择兼容的依赖版本。

对于 TypeScript 项目,下面的命令还会安装 @types/node@24,以匹配该运行时并满足 Vitest 5 的 peer 依赖。这会替换由 create-next-app 生成的项目中的 @types/node@20 依赖。仅升级 Node.js 并不会更新这些类型定义。

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

bash · pnpm
# 使用 TypeScript
pnpm add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths @types/node@24
# 使用 JavaScript
pnpm add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom
bash · npm
# 使用 TypeScript
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths @types/node@24
# 使用 JavaScript
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom
bash · yarn
# 使用 TypeScript
yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths @types/node@24
# 使用 JavaScript
yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom
bash · bun
# 使用 TypeScript
bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths @types/node@24
# 使用 JavaScript
bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom

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

vitest.config.mts
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',
  },
})
vitest.config.js
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.json
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "test": "vitest"
  }
}

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

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

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

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

export default function Page() {
  return (
    <div>
      <h1>Home</h1>
      <Link href="/about">About</Link>
    </div>
  )
}
app/page.jsx
import Link from 'next/link'

export default function Page() {
  return (
    <div>
      <h1>Home</h1>
      <Link href="/about">About</Link>
    </div>
  )
}
__tests__/page.test.tsx
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__/page.test.jsx
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 路由器内部。

Vitest 的 jsdom 设置不会运行 App Router 的导航运行时。请使用显式 mock 来测试你自己的 onNavigate 逻辑或使用 useLinkStatus 的 UI。要验证导航和待处理状态,请针对正在运行的 Next.js 应用程序使用 Playwright E2E 测试。

pages/index.tsx
import Link from 'next/link'

export default function Page() {
  return (
    <div>
      <h1>Home</h1>
      <Link href="/about">About</Link>
    </div>
  )
}
pages/index.jsx
import Link from 'next/link'

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

test('Page', () => {
  render(<Page />)
  expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined()
})
__tests__/index.test.jsx
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'
import Page from '../pages/index'

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

运行测试#

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

bash · pnpm
pnpm test
bash · npm
npm run test
bash · yarn
yarn test
bash · bun
bun run test

其他资源#

你可能会发现以下资源很有帮助:

Next.js 中文网