如何使用 Next.js 设置 Vitest
了解如何使用 Next.js 设置 Vitest 以进行单元测试。
Vitest 和 React Testing Library 经常一起用于单元测试。本指南将向你展示如何使用 Next.js 设置 Vitest,并编写你的第一个测试。
小知识: 由于
asyncServer Component 在 React 生态系统中是新增特性,Vitest 目前不支持它们。虽然你仍然可以为同步的 Server Component 和 Client Component 运行单元测试,但我们建议对async组件使用 E2E 测试。
快速开始#
你可以将 create-next-app 与 Next.js 的 with-vitest 示例结合使用,以快速上手:
pnpm create next-app --example with-vitest with-vitest-appnpx create-next-app@latest --example with-vitest with-vitest-appyarn create next-app --example with-vitest with-vitest-appbun 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 及以下包安装为开发依赖:
# 使用 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# 使用 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# 使用 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# 使用 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 文件,并添加以下选项:
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',
},
})import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
},
})有关配置 Vitest 的更多信息,请参阅 Vitest 配置文档。
然后,在你的 package.json 中添加一个 test 脚本:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"test": "vitest"
}
}当你运行 npm run test 时,Vitest 默认会监视项目中的变化。
创建你的第一个 Vitest 单元测试#
通过创建一个测试来确认 <Page /> 组件是否成功渲染了一个标题,从而检查一切是否正常工作:
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}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()
})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 测试。
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}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()
})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()
})运行测试#
然后,运行以下命令来执行你的测试:
pnpm testnpm run testyarn testbun run test其他资源#
你可能会发现以下资源很有帮助: