Instructions for Cypress Component Testing and JavaScript
Configuring your component tests with Cypress and JavaScript
-
In the root level of your testing folder, install the
@axe-core/watcherpackage and all of its dependencies usingnpmor your preferred package manager (for example,yarnorpnpm).npm install --save-dev @axe-core/watcher -
In
cypress.config.js, import thecypressConfig()function from the@axe-core/watcherpackage and wrap your config with it. Set thetestingTypesarray to includecomponent:const { defineConfig } = require('cypress'); const { cypressConfig } = require('@axe-core/watcher/cypress/config'); const ACCESSIBILITY_API_KEY = process.env.ACCESSIBILITY_API_KEY const PROJECT_ID = process.env.PROJECT_ID module.exports = 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', //}, }, }) );Be sure to set
ACCESSIBILITY_API_KEYandPROJECT_IDin your environment to your personal API key (found in your axe Account, API KEYS tab) and your project ID (shown at the top of these instructions when you created your project or available from the Projects page by choosing Configure project under Settings). -
In your Cypress component support file (commonly called
cypress/support/component.js), import the@axe-core/watcherpackagecypressCommandsand callaxeWatcherFlush()after each test:require('@axe-core/watcher/cypress/support'); afterEach(() => { cy.axeWatcherFlush(); }); -
You're all done. Run your component tests and check your results. Please note
@axe-core/watcheronly runs in Chrome for Testing or Chromium and should only be used in Cypress run mode. It may be used with--headless=newor--headed(for example,cypress run --headed --browser=chrome-for-testing --component).
