Testing Ionic React
This document provides an overview of how to test an application built with @ionic/react. It covers the basics of testing with React, as well as the specific tools and libraries developers can use to test their applications.
Introduction
テストはアプリが意図どおりに動作することを確認するための開発工程です。Ionic CLI で生成したアプリでは、ユニットテストに Vitest と React Testing Library、E2E テストに Playwright が設定されています。ユニットテストは src 内の対象コードの隣に、E2E テストは e2e に配置します。
生成される package.json には、これらのテストを実行するスクリプトが含まれています。
| Script | Description |
|---|---|
npm test | ユニットテストを1回実行し、続いて E2E テストを実行します。 |
npm run test:unit | ユニットテストを1回実行します。 |
npm run test:unit:watch | ユニットテストを実行し、ファイルの変更時に再実行します。 |
npm run test:e2e | 開発サーバーを起動して E2E テストを実行します。 |
Playwright のブラウザーは npm パッケージとは別にダウンロードします。初回の E2E テストの前に npx playwright install を一度実行してください。
Types of Tests
There are two types of tests that can be written:
Unit Tests: Unit tests are used to test individual functions and components in isolation. Jest, Vitest and React Testing Library are commonly used for unit testing.
E2E テストでは、実行中のアプリで複数のコンポーネントがどう連携するかを検証します。よく使われるツールに Playwright と Cypress があります。