跳到内容

如何在 Next.js 中设置 Cypress

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

警告

  • Cypress 低于 13.6.3 的版本不支持 TypeScript 5 版本 以及 moduleResolution:"bundler"。但是,此问题已在 Cypress 13.6.3 及更高版本中解决。cypress v13.6.3

手动设置

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

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

将 Cypress open 命令添加到 package.json 脚本字段

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

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

终端
npm run cypress:open

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

创建你的第一个 Cypress E2E 测试

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

cypress.config.ts
import { defineConfig } from 'cypress'
 
export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {},
  },
})

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

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

添加一个测试以检查您的导航是否正常工作

cypress/e2e/app.cy.js
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 测试套件。

须知

  • 您可以通过将 baseUrl: 'https://:3000' 添加到 cypress.config.js 配置文件中来使用 cy.visit("/") 而不是 cy.visit("https://:3000/")
  • 或者,您可以安装 start-server-and-test 包,将 Next.js 生产服务器与 Cypress 一起运行。安装后,将 "test": "start-server-and-test start https://:3000 cypress" 添加到您的 package.json 脚本字段。请记住在新更改后重建您的应用程序。

创建你的第一个 Cypress 组件测试

组件测试构建并挂载一个特定的组件,而无需捆绑整个应用程序或启动服务器。

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

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

cypress.config.ts
import { defineConfig } from 'cypress'
 
export default defineConfig({
  component: {
    devServer: {
      framework: 'next',
      bundler: 'webpack',
    },
  },
})

假设与上一节中的组件相同,添加一个测试以验证组件是否呈现预期输出

cypress/component/about.cy.js
import AboutPage from '../../pages/about'
 
describe('<AboutPage />', () => {
  it('should render and display expected content', () => {
    // Mount the React component for the About page
    cy.mount(<AboutPage />)
 
    // The new page should contain an h1 with "About page"
    cy.get('h1').contains('About')
 
    // Validate that a link with the expected URL is present
    // *Following* the link is better suited to an E2E test
    cy.get('a[href="/"]').should('be.visible')
  })
})

须知:

  • Cypress 目前不支持 async 服务器组件的组件测试。我们建议使用 E2E 测试。
  • 由于组件测试不需要 Next.js 服务器,因此像 <Image /> 这样依赖服务器可用的功能可能无法直接运行。

运行组件测试

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

持续集成 (CI)

除了交互式测试,您还可以使用 cypress run 命令以无头模式运行 Cypress,这更适合 CI 环境

package.json
{
  "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 和持续集成的更多信息