在 Next.js 中设置 Vitest
Vite 和 React Testing Library 经常一起用于单元测试。本指南将向您展示如何在 Next.js 中设置 Vitest 并编写您的第一个测试。
须知: 由于
async
服务器组件是 React 生态系统中的新成员,Vitest 当前不支持它们。尽管您仍然可以为同步的服务器和客户端组件运行单元测试,但我们建议您为async
组件使用E2E测试。
快速开始
您可以使用 create-next-app
与 Next.js with-vitest 示例快速开始:
bash
npx create-next-app@latest --example with-vitest with-vitest-app
手动设置
要手动设置 Vitest,请安装 vitest
和以下包作为开发依赖项:
bash
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react
# 或者
yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react
# 或者
pnpm install -D vitest @vitejs/plugin-react jsdom @testing-library/react
# 或者
bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react
在项目根目录创建一个 vitest.config.ts|js
文件,并添加以下选项:
ts
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
},
})
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
脚本:
json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"test": "vitest"
}
}
当您运行 npm run test
时,Vitest 将默认监视您项目中的更改。
创建你的第一个 Vitest 单元测试
通过创建一个测试来检查 <Page />
组件是否成功渲染了一个标题,来验证一切是否正常工作:
运行你的测试
然后,运行以下命令来运行你的测试:
bash
npm run test
# 或
yarn test
# 或
pnpm test
# 或
bun test
其他资源
你可能会发现这些资源很有帮助: