Vue Test Utils v2 のテストガイドについて、多くの開発者は API の呼び出しレベルにとどまっている。本記事では本番環境の観点から、実際に遭遇する問題と解決策を議論する。
基本原理
基本的な実装方法を見てみよう:
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 { reactive, toRefs, computed } from 'vue'
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] })
const doubled = computed(() => state.count * 2)
function increment() {
state.count++
state.history.push(state.count)
}
return { ...toRefs(state), doubled, increment }
}
このパターンは大規模プロジェクトで非常に実用的で、保守コストを大幅に削減できる。
プロジェクト実践
実際のプロジェクトでは使い方がより複雑になる:
javascript
import { ref, computed, watch, onMounted } from 'vue'
export default {
setup() {
const count = ref(0)
const doubled = computed(() => count.value * 2)
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`)
})
onMounted(() => { console.log('组件已挂载') })
return { count, doubled }
}
}
このアプローチにより、コードのテスト可能性とスケーラビリティが向上する。
ベストプラクティス
全体のサンプルを以下に示す:
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('欢迎回来')
})
})
境界条件の処理に注意してください。これは本番環境で非常に重要です。
まとめ
- Vue Test Utils v2 のテストガイドは銀の弾丸ではなく、プロジェクトの規模と技術スタックに応じて選ぶ必要がある
- 基礎的な原理を理解することは、API を暗記することより重要だ
- 本番環境で使用する前に必ず互換性を確認してください
