跳至內容

使用 Next.js 設定 Cypress (在 Next.js 中設定 Cypress)

Cypress 是一個用於端到端 (E2E)元件測試的測試執行器。本頁將說明如何使用 Next.js 設定 Cypress 並撰寫您的第一個測試。

警告

  • 對於元件測試,Cypress 目前不支援 Next.js 版本 14async 伺服器元件。這些問題正在追蹤中。目前,元件測試適用於 Next.js 版本 13,我們建議對 async 伺服器元件使用端到端測試。
  • 低於 13.6.3 版本的 Cypress 不支援 TypeScript 版本 5 搭配 moduleResolution:"bundler" 使用。但是,此問題已在 Cypress 13.6.3 及更高版本中解決。cypress v13.6.3

快速入門

您可以使用內建 with-cypress 範例create-next-app 快速開始。

終端機
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.jsonscripts 欄位

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

第一次執行 Cypress 以開啟 Cypress 測試套件

終端機
npm run cypress:open

您可以選擇設定端到端測試和/或元件測試。選擇任一選項將會自動在您的專案中建立 cypress.config.js 檔案和 cypress 資料夾。

建立您的第一個 Cypress 端對端測試

請確認您的 cypress.config.js 檔案包含以下設定

cypress.config.ts
import { defineConfig } from 'cypress'
 
export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {},
  },
})
cypress.config.js
const { defineConfig } = require('cypress')
 
module.exports = 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://127.0.0.1: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')
  })
})

執行端對端測試

Cypress 將模擬使用者操作您的應用程式,這需要您的 Next.js 伺服器正在執行。我們建議您針對您的正式程式碼執行測試,以更貼近應用程式的實際行為。

執行 npm run build && npm run start 來建置您的 Next.js 應用程式,然後在另一個終端機視窗中執行 npm run cypress:open 以啟動 Cypress 並執行您的端對端測試套件。

注意事項

  • 您可以透過在 cypress.config.js 設定檔中新增 baseUrl: 'https://127.0.0.1:3000',使用 cy.visit("/") 來取代 cy.visit("https://127.0.0.1:3000/")
  • 或者,您可以安裝 start-server-and-test 套件,以便與 Cypress 一起執行 Next.js 正式伺服器。安裝完成後,在您的 package.json 指令碼欄位中新增 "test": "start-server-and-test start https://127.0.0.1:3000 cypress"。請記住在程式碼變更後重新建置您的應用程式。

建立您的第一個 Cypress 元件測試

元件測試會建置並掛載特定元件,而無需打包整個應用程式或啟動伺服器。

在 Cypress 應用程式中選取「元件測試」,然後選擇「Next.js」作為您的前端框架。您的專案中將會建立一個 cypress/component 資料夾,並且 cypress.config.js 檔案將會更新以啟用元件測試。

請確認您的 cypress.config.js 檔案包含以下設定

cypress.config.ts
import { defineConfig } from 'cypress'
 
export default defineConfig({
  component: {
    devServer: {
      framework: 'next',
      bundler: 'webpack',
    },
  },
})
cypress.config.js
const { defineConfig } = require('cypress')
 
module.exports = 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 伺服器元件的元件測試。我們建議使用端對端測試。
  • 由於元件測試不需要 Next.js 伺服器,因此依賴伺服器才能運作的功能 (例如 <Image />) 可能無法直接使用。

執行元件測試

在您的終端機中執行 npm run cypress:open 以啟動 Cypress 並執行您的元件測試套件。

持續整合 (CI)

除了互動式測試之外,您也可以使用 `cypress run` 指令以無頭模式執行 Cypress,這更適合 CI 環境。

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

您可以從以下資源進一步了解 Cypress 與持續整合: