CypressコンポーネントテストとTypeScriptの手引き

This page is not available in the language you requested. You have been redirected to the English version of the page.
Link to this page copied to clipboard

CypressとTypeScriptでコンポーネントテストを設定する

Not for use with personal data
  1. テストフォルダのルートレベルに、 @axe-core/watcher パッケージとそのすべての依存関係をインストールします。 npm またはお好みのパッケージマネージャを使用します(例: yarn または pnpm)。

    npm install --save-dev @axe-core/watcher
  2. の中で cypress.config.tscypressConfig() パッケージから @axe-core/watcher 関数をインポートし、設定をそれでラップします。 testingTypes 配列を以下を含むように設定します component

    import { defineConfig } from 'cypress';
    import { cypressConfig } from '@axe-core/watcher/cypress/config';
    
    const ACCESSIBILITY_API_KEY = process.env.ACCESSIBILITY_API_KEY
    const PROJECT_ID = process.env.PROJECT_ID
    
    export default defineConfig(
      cypressConfig({
        axe: {
          apiKey: ACCESSIBILITY_API_KEY,
          projectId: PROJECT_ID,
          testingTypes: ['component']
        },
        // Your existing Cypress configuration code here
        component: {
          // Your component testing configuration
          
          // For example:
          
          // devServer: {
          //  framework: 'react',
          //  bundler: 'webpack',
          //},
        },
      })
    );

    必ず環境で ACCESSIBILITY_API_KEYPROJECT_ID を設定し、自分のAPIキー(axeアカウントで見つかります、 **API KEYS** タブ)とプロジェクトID(プロジェクトを作成したときのこれらの指示の上部に表示されるか、 **プロジェクトの構成** を選択してプロジェクトページから利用可能です) **設定**の下にあります。

  3. Cypressコンポーネントサポートファイル(一般的にcypress/support/component.tsと呼ばれる)で、 @axe-core/watcher/cypress/support をインポートし、 axeWatcherFlush() 各テスト後に

    import '@axe-core/watcher/cypress/support';
    
    afterEach(() => {
      cy.axeWatcherFlush();
    });
  4. これで完了です。コンポーネントテストを実行し、結果を確認してください。次の点に注意してください: @axe-core/watcher はChrome for TestingまたはChromiumでのみ実行でき、Cypressの実行モードでのみ使用すべきです。それは --headless=new または --headed (例えば、 cypress run --headed --browser=chrome-for-testing --component)と一緒に使用される可能性があります。