如何在 Next.js 中配置 Cypress

Cypress 是一个用于 端到端测试 (E2E)组件测试 的测试运行器。本文将展示如何在 Next.js 中配置 Cypress 并编写第一个测试。

警告:

  • Cypress 13.6.3 以下版本不支持 TypeScript 5moduleResolution:"bundler" 配置。该问题已在 Cypress 13.6.3 及更高版本中修复。cypress v13.6.3

快速开始

您可以使用 create-next-app 配合 with-cypress 示例 快速开始:

终端
npx create-next-app@latest --example with-cypress with-cypress-app

手动配置

要手动配置 Cypress,请安装 cypress 作为开发依赖:

终端
npm install -D cypress
# 或
yarn add -D cypress
# 或
pnpm install -D cypress

将 Cypress 的 open 命令添加到 package.json 的 scripts 字段中:

package.json
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "cypress:open": "cypress open"
  }
}

首次运行 Cypress 以打开测试套件:

终端
npm run cypress:open

您可以选择配置 E2E 测试 和/或 组件测试。选择任一选项将自动在项目中创建 cypress.config.js 文件和 cypress 文件夹。

创建第一个 Cypress E2E 测试

确保 cypress.config 文件包含以下配置:

import { defineConfig } from 'cypress'

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {},
  },
})

然后创建两个新的 Next.js 文件:

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

export default function Page() {
  return (
    <div>
      <h1>首页</h1>
      <Link href="/about">关于</Link>
    </div>
  )
}
app/about/page.js
import Link from 'next/link'

export default function Page() {
  return (
    <div>
      <h1>关于</h1>
      <Link href="/">首页</Link>
    </div>
  )
}

添加测试以验证导航功能:

cypress/e2e/app.cy.js
describe('导航测试', () => {
  it('应能跳转到关于页面', () => {
    // 从首页开始
    cy.visit('http://localhost:3000/')

    // 找到包含 "about" 的链接并点击
    cy.get('a[href*="about"]').click()

    // 新 URL 应包含 "/about"
    cy.url().should('include', '/about')

    // 新页面应包含 "About" 标题
    cy.get('h1').contains('About')
  })
})

运行 E2E 测试

Cypress 会模拟用户操作,因此需要 Next.js 服务器处于运行状态。建议针对生产环境代码运行测试以更接近真实场景。

先运行 npm run build && npm run start 构建 Next.js 应用,然后在另一个终端窗口运行 npm run cypress:open 启动 Cypress 并执行 E2E 测试套件。

须知:

  • 可以通过在 cypress.config.js 中添加 baseUrl: 'http://localhost:3000' 配置,使用 cy.visit("/") 替代完整 URL
  • 或者安装 start-server-and-test 包来同时运行 Next.js 生产服务器和 Cypress。安装后,在 package.json 的 scripts 字段添加 "test": "start-server-and-test start http://localhost:3000 cypress"。注意修改代码后需要重新构建应用

创建第一个 Cypress 组件测试

组件测试可以单独构建和挂载特定组件,无需启动完整应用或服务器。

在 Cypress 应用中选择 组件测试,然后选择 Next.js 作为前端框架。这将在项目中创建 cypress/component 文件夹并更新 cypress.config.js 文件以启用组件测试。

确保 cypress.config 文件包含以下配置:

import { defineConfig } from 'cypress'

export default defineConfig({
  component: {
    devServer: {
      framework: 'next',
      bundler: 'webpack',
    },
  },
})

假设使用前一节的组件,添加测试验证组件渲染:

cypress/component/about.cy.tsx
import Page from '../../app/page'

describe('<Page />', () => {
  it('应正确渲染并显示预期内容', () => {
    // 挂载首页组件
    cy.mount(<Page />)

    // 页面应包含 "Home" 标题
    cy.get('h1').contains('Home')

    // 验证包含预期 URL 的链接可见
    // 实际跳转测试更适合 E2E 测试
    cy.get('a[href="/about"]').should('be.visible')
  })
})

须知:

  • Cypress 目前不支持对 async 服务端组件的组件测试,建议使用 E2E 测试
  • 由于组件测试不需要 Next.js 服务器,依赖服务器的功能(如 <Image /> 组件)可能无法直接使用

运行组件测试

在终端运行 npm run cypress:open 启动 Cypress 并执行组件测试套件。

持续集成 (CI)

除了交互式测试,还可以使用 cypress run 命令在 CI 环境中运行无头测试:

package.json
{
  "scripts": {
    //...
    "e2e": "start-server-and-test dev http://localhost:3000 \"cypress open --e2e\"",
    "e2e:headless": "start-server-and-test dev http://localhost:3000 \"cypress run --e2e\"",
    "component": "cypress open --component",
    "component:headless": "cypress run --component"
  }
}

更多 Cypress 持续集成资源:

On this page