設定 Cypress 與 Next.js
Cypress 是一個用於端對端 (E2E) 和元件測試的測試執行器。本頁面將說明如何設定 Cypress 與 Next.js,並編寫你的第一個測試。
警告
- 低於 13.6.3 版本的 Cypress 不支援具有
moduleResolution:"bundler"
的 TypeScript 版本 5。然而,此問題已在 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
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 測試和/或元件測試。選擇任何一個選項都會自動在你的專案中建立 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 Home() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
import Link from 'next/link'
export default function About() {
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://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')
})
})
執行 E2E 測試
Cypress 將模擬使用者導航你的應用程式,這需要你的 Next.js 伺服器正在運行。我們建議針對你的生產程式碼執行測試,以更接近你的應用程式的行為方式。
執行 npm run build && npm run start
以建置你的 Next.js 應用程式,然後在另一個終端機視窗中執行 npm run cypress:open
以啟動 Cypress 並執行你的 E2E 測試套件。
要知道
- 你可以使用
cy.visit("/")
而不是cy.visit("https://127.0.0.1:3000/")
,方法是將baseUrl: 'https://127.0.0.1:3000'
新增至cypress.config.js
設定檔。- 或者,你可以安裝
start-server-and-test
套件,以結合 Cypress 運行 Next.js 生產伺服器。安裝完成後,將"test": "start-server-and-test start https://127.0.0.1:3000 cypress"
新增至你的package.json
scripts 欄位。請記得在新變更後重建你的應用程式。
建立你的第一個 Cypress 元件測試
元件測試會建置和掛載特定的元件,而無需捆綁你的整個應用程式或啟動伺服器。
在 Cypress 應用程式中選擇元件測試,然後選擇 Next.js 作為你的前端框架。將在你的專案中建立 cypress/component
資料夾,並且將更新 cypress.config.js
檔案以啟用元件測試。
確保你的 cypress.config
檔案具有以下設定
import { defineConfig } from 'cypress'
export default defineConfig({
component: {
devServer: {
framework: 'next',
bundler: 'webpack',
},
},
})
假設與前一節相同的元件,新增一個測試以驗證元件是否呈現預期的輸出
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 環境
{
"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 和持續整合的資訊
這有幫助嗎?