Skip to content
⚠️ This article was written in 2020. Some content may be outdated.

Cypress コンポーネントテスト入門

Cypress のコンポーネントテスト入門は、フロントエンド開発での活用が広がりつつある。本記事では実際のプロジェクトに基づき、その中核となる原理とベストプラクティスを深掘りする。

基本的な使い方 ​

以下に完全なサンプルを示す:

javascript
import { test, expect } from '@playwright/test'

test.describe('用户登录流程', () => {
  test('成功登录跳转到首页', async ({ page }) => {
    await page.goto('/login')
    await page.fill('[data-testid="email"]', 'admin@example.com')
    await page.fill('[data-testid="password"]', 'admin123')
    await page.click('[data-testid="submit-btn"]')
    await expect(page).toHaveURL('/dashboard')
    await expect(page.locator('.welcome')).toContainText('欢迎回来')
  })
})

境界条件の処理に注意すること。これは本番環境では極めて重要だ。

応用的な使い方 ​

重要なのはコアとなるロジックを理解することだ:

javascript
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { LoginForm } from './LoginForm'

describe('LoginForm', () => {
  it('提交有效的登录表单', async () => {
    const onSubmit = jest.fn()
    render(<LoginForm onSubmit={onSubmit} />)
    await userEvent.type(screen.getByLabelText(/邮箱/), 'user@example.com')
    await userEvent.type(screen.getByLabelText(/密码/), 'password123')
    await userEvent.click(screen.getByRole('button', { name: /登录/ }))
    await waitFor(() => {
      expect(onSubmit).toHaveBeenCalledWith({
        email: 'user@example.com', password: 'password123'
      })
    })
  })
})

パフォーマンス最適化は具体的なシナリオに応じて行う必要があり、すべてのケースで過度な最適化が必要なわけではない。

実践事例 ​

以下のアプローチで改善できる:

javascript
import { test, expect } from '@playwright/test'

test.describe('用户登录流程', () => {
  test('成功登录跳转到首页', async ({ page }) => {
    await page.goto('/login')
    await page.fill('[data-testid="email"]', 'admin@example.com')
    await page.fill('[data-testid="password"]', 'admin123')
    await page.click('[data-testid="submit-btn"]')
    await expect(page).toHaveURL('/dashboard')
    await expect(page.locator('.welcome')).toContainText('欢迎回来')
  })
})

このアプローチは本番環境で半年以上安定稼働しており、実際に検証済みだ。

パフォーマンス最適化 ​

まず基本的な実装方法を見てみよう:

javascript
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { LoginForm } from './LoginForm'

describe('LoginForm', () => {
  it('提交有效的登录表单', async () => {
    const onSubmit = jest.fn()
    render(<LoginForm onSubmit={onSubmit} />)
    await userEvent.type(screen.getByLabelText(/邮箱/), 'user@example.com')
    await userEvent.type(screen.getByLabelText(/密码/), 'password123')
    await userEvent.click(screen.getByRole('button', { name: /登录/ }))
    await waitFor(() => {
      expect(onSubmit).toHaveBeenCalledWith({
        email: 'user@example.com', password: 'password123'
      })
    })
  })
})

このコードは基本的な使い方を示している。実際のプロジェクトではエラー処理や境界条件も考慮する必要がある。

よくある落とし穴 ​

これをベースにさらに最適化できる:

javascript
import { test, expect } from '@playwright/test'

test.describe('用户登录流程', () => {
  test('成功登录跳转到首页', async ({ page }) => {
    await page.goto('/login')
    await page.fill('[data-testid="email"]', 'admin@example.com')
    await page.fill('[data-testid="password"]', 'admin123')
    await page.click('[data-testid="submit-btn"]')
    await expect(page).toHaveURL('/dashboard')
    await expect(page.locator('.welcome')).toContainText('欢迎回来')
  })
})

このパターンは大規模なプロジェクトで非常に実用的で、保守コストを大幅に下げられる。

まとめ ​

  • コード例は参考程度であり、業務のシナリオに合わせて調整する必要がある
  • Cypress のコンポーネントテスト入門は銀の弾丸ではなく、プロジェクトの規模と技術スタックに応じて選択する必要がある
  • API を覚えることよりも、背後にある原理を理解することが重要だ

MIT Licensed