跳到內容

如何使用 Next.js 設定 Cypress

Cypress是一個用於端到端 (E2E)元件測試的測試執行器。本頁將向你展示如何使用 Next.js 設定 Cypress 並編寫你的第一個測試。

警告

  • 低於 13.6.3 版本的 Cypress 不支援帶 moduleResolution:"bundler"TypeScript 5。但是,此問題已在 Cypress 13.6.3 及更高版本中解決。Cypress v13.6.3

快速入門

你可以使用 create-next-appwith-cypress 示例快速開始。

終端
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 指令碼欄位。

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 檔案:

app/page.js
import Link from 'next/link'
 
export default function Page() {
  return (
    <div>
      <h1>Home</h1>
      <Link href="/about">About</Link>
    </div>
  )
}
app/about/page.js
import Link from 'next/link'
 
export default function Page() {
  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 測試套件。

須知

  • 透過在 cypress.config.js 配置檔案中新增 baseUrl: 'https://:3000',你可以使用 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.tsx
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 目前不支援 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 和持續整合的資訊: