配置 Next.js 的 Cypress
Cypress 是一个用于 端到端 (E2E) 和 组件测试 的测试运行器。 本页将向你展示如何配置 Next.js 的 Cypress 并编写你的第一个测试。
警告
- 低于 13.6.3 的 Cypress 版本不支持 TypeScript version 5 使用
moduleResolution:"bundler"。 但是,此问题已在 Cypress 13.6.3 及更高版本中得到解决。 cypress v13.6.3
快速开始
你可以使用 create-next-app 与 with-cypress example 示例快速开始。
npx create-next-app@latest --example with-cypress with-cypress-app手动设置
要手动设置 Cypress,请安装 cypress 作为开发依赖项
npm install -D cypress
# or
yarn add -D cypress
# or
pnpm install -D cypress将 Cypress open 命令添加到 package.json scripts 字段
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "cypress:open": "cypress open"
  }
}首次运行 Cypress 以打开 Cypress 测试套件
npm run cypress:open你可以选择配置 E2E Testing 和/或 Component Testing。 选择其中任何一个选项都将自动创建一个 cypress.config.js 文件和项目中的 cypress 文件夹。
创建你的第一个 Cypress E2E 测试
确保你的 cypress.config 文件具有以下配置
import { defineConfig } from 'cypress'
 
export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {},
  },
})然后,创建两个新的 Next.js 文件
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>About</h1>
      <Link href="/">Home</Link>
    </div>
  )
}添加一个测试来检查你的导航是否正常工作
describe('Navigation', () => {
  it('should navigate to the about page', () => {
    // Start from the index page
    cy.visit('https://:3000/')
 
    // Find a link with an href attribute containing "about" and click it
    cy.get('a[href*="about"]').click()
 
    // The new url should include "/about"
    cy.url().should('include', '/about')
 
    // The new page should contain an h1 with "About"
    cy.get('h1').contains('About')
  })
})运行 E2E 测试
Cypress 将模拟用户导航你的应用程序,这需要你的 Next.js 服务器正在运行。 我们建议针对你的生产代码运行测试,以更接近地模拟你的应用程序的行为方式。
运行 npm run build && npm run start 构建你的 Next.js 应用程序,然后在另一个终端窗口中运行 npm run cypress:open 启动 Cypress 并运行你的 E2E 测试套件。
须知
- 你可以使用
cy.visit("/")而不是cy.visit("https://:3000/"),方法是将baseUrl: 'https://:3000'添加到cypress.config.js配置文件。- 或者,你可以安装
start-server-and-test包,以结合 Cypress 运行 Next.js 生产服务器。 安装后,将"test": "start-server-and-test start https://:3000 cypress"添加到你的package.jsonscripts 字段。 记住在新更改后重建你的应用程序。
创建你的第一个 Cypress 组件测试
组件测试构建并挂载特定的组件,而无需捆绑你的整个应用程序或启动服务器。
在 Cypress 应用程序中选择 Component Testing,然后选择 Next.js 作为你的前端框架。 一个 cypress/component 文件夹将在你的项目中创建,并且 cypress.config.js 文件将被更新以启用组件测试。
确保你的 cypress.config 文件具有以下配置
import { defineConfig } from 'cypress'
 
export default defineConfig({
  component: {
    devServer: {
      framework: 'next',
      bundler: 'webpack',
    },
  },
})假设与上一节相同的组件,添加一个测试来验证组件是否正在渲染预期的输出
import Page from '../../app/page'
 
describe('<Page />', () => {
  it('should render and display expected content', () => {
    // Mount the React component for the Home page
    cy.mount(<Page />)
 
    // The new page should contain an h1 with "Home"
    cy.get('h1').contains('Home')
 
    // Validate that a link with the expected URL is present
    // Following the link is better suited to an E2E test
    cy.get('a[href="/about"]').should('be.visible')
  })
})须知:
- Cypress 目前不支持 Server Components 的组件测试。 我们建议使用 E2E 测试。
- 由于组件测试不需要 Next.js 服务器,因此诸如
<Image />之类的依赖于服务器可用的功能可能无法开箱即用。
运行组件测试
在你的终端中运行 npm run cypress:open 启动 Cypress 并运行你的组件测试套件。
持续集成 (CI)
除了交互式测试外,你还可以使用 cypress run 命令以无头模式运行 Cypress,这更适合 CI 环境
{
  "scripts": {
    //...
    "e2e": "start-server-and-test dev https://:3000 \"cypress open --e2e\"",
    "e2e:headless": "start-server-and-test dev https://:3000 \"cypress run --e2e\"",
    "component": "cypress open --component",
    "component:headless": "cypress run --component"
  }
}你可以从以下资源中了解更多关于 Cypress 和持续集成的知识
这有帮助吗?