|
2 | 2 |
|
3 | 3 | import '@percy/cypress'
|
4 | 4 | import type { SnapshotOptions } from '@percy/core'
|
| 5 | +import 'cypress-axe' |
5 | 6 |
|
6 | 7 | export interface CustomSnapshotOptions extends SnapshotOptions{
|
7 | 8 | /**
|
@@ -54,6 +55,54 @@ declare global {
|
54 | 55 | }
|
55 | 56 | }
|
56 | 57 |
|
| 58 | +function setupAxeAndCheckA11y () { |
| 59 | + if (Cypress.testingType === 'component') { |
| 60 | + Cypress.Commands.add('injectAxe', () => { |
| 61 | + // this is a work around for the issue with require.resolve in CT |
| 62 | + // described here: https://github.com/component-driven/cypress-axe/issues/134 |
| 63 | + cy.window({ log: false }).then((window) => { |
| 64 | + const axe = require('axe-core/axe.js') |
| 65 | + const script = window.document.createElement('script') |
| 66 | + |
| 67 | + script.innerHTML = axe.source |
| 68 | + window.document.head.appendChild(script) |
| 69 | + }) |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + cy.injectAxe() |
| 74 | + |
| 75 | + if (Cypress.testingType === 'component') { |
| 76 | + // since components are isolated fragments, right off the bat |
| 77 | + // there are some things we should avoid checking |
| 78 | + cy.configureAxe({ |
| 79 | + rules: [ |
| 80 | + { |
| 81 | + id: 'html-has-lang', |
| 82 | + enabled: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + id: 'landmark-one-main', |
| 86 | + enabled: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + id: 'page-has-heading-one', |
| 90 | + enabled: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + id: 'region', |
| 94 | + enabled: false, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + Cypress.log({ displayName: '♿️ Accessibility Check ♿️' }) |
| 101 | + |
| 102 | + // passing undefined here so that we can set the final boolean to ignore failures for now |
| 103 | + cy.checkA11y(undefined, undefined, undefined, true) |
| 104 | +} |
| 105 | + |
57 | 106 | class ElementOverrideManager {
|
58 | 107 | private mutationStack: Array<MutationRecord> | undefined = undefined
|
59 | 108 |
|
@@ -189,7 +238,7 @@ const applySnapshotMutations = ({
|
189 | 238 | })
|
190 | 239 | }
|
191 | 240 |
|
192 |
| -export const installCustomPercyCommand = ({ before, elementOverrides }: {before?: () => void, elementOverrides?: CustomSnapshotOptions['elementOverrides'] } = {}) => { |
| 241 | +export const installCustomPercyCommand = ({ before, elementOverrides }: {before?: () => void, elementOverrides?: CustomSnapshotOptions['elementOverrides'], isComponentTesting?: boolean } = {}) => { |
193 | 242 | /**
|
194 | 243 | * A custom Percy command that allows for additional mutations prior to snapshot generation. Mutations will be
|
195 | 244 | * reset after snapshot generation so that the AUT is not polluted after the command executes.
|
@@ -239,6 +288,11 @@ export const installCustomPercyCommand = ({ before, elementOverrides }: {before?
|
239 | 288 | // that is representative of the snapshot that would be taken by Percy and can be
|
240 | 289 | // used for validation during development.
|
241 | 290 | if (Cypress.config().isInteractive) {
|
| 291 | + // since Percy snapshots represent visually unique states in the application |
| 292 | + // doing an accessibility check here should cover all situations that need to a11y checks |
| 293 | + |
| 294 | + setupAxeAndCheckA11y() |
| 295 | + |
242 | 296 | return applySnapshotMutations({
|
243 | 297 | ...snapshotMutationOptions,
|
244 | 298 | log: 'percy: skipping snapshot in interactive mode',
|
|
0 commit comments