テスト
@ionic/vue で構築したアプリのテスト方法を説明します。Ionic CLI で生成したアプリでは、ユニットテストに Vitest と Vue Test Utils、E2E テストに Playwright が設定されています。ユニットテストは tests/unit、E2E テストは tests/e2e に配置します。
生成される package.json には、これらのテストを実行するスクリプトが含まれています。
| Script | Description |
|---|---|
npm test | ユニットテストを1回実行し、続いて E2E テストを実行します。 |
npm run test:unit | ユニットテストを1回実行します。 |
npm run test:unit:watch | ユニットテストを実行し、ファイルの変更時に再実行します。 |
npm run test:e2e | 開発サーバーを起動して E2E テストを実行します。 |
note
Playwright のブラウザーは npm パッケージとは別にダウンロードします。初回の E2E テストの前に npx playwright install を一度実行してください。
ユニットテスト
コンポーネントの待機
Ionic コンポーネントの DOM に対してアサーションを行う前にレンダリングを待つ必要がある場合は、@ionic/coreからエクスポートされるcomponentOnReadyヘルパーを使用してください。el.componentOnReady()を直接呼び出さないでください。@ionic/vueは Stencil のカスタム要素ビルドを使用しており、このメソッドは要素上には存在しません。このヘルパーは代わりに 1 フレーム待機し、コンポーネントの内部コンテンツがレンダリングされる機会を与えます。
import { componentOnReady } from '@ionic/core';
import { mount } from '@vue/test-utils';
import { expect, test } from 'vitest';
import Example from './Example.vue';
test('renders the submit button', async () => {
const wrapper = mount(Example);
const button = wrapper.find('ion-button').element;
await new Promise<void>((resolve) => componentOnReady(button, () => resolve()));
expect(button.textContent).toContain('Submit');
});