Skip to content

Commit 29343e8

Browse files
authored
test: initial cypress-axe setup for e2e and CT (#22925)
* initial cypress-axe setup for e2e and CT * extract axe setup, use isComponentTesting * ensure CT sets isComponentTesting to percy command * test flake fix from lachlan * only run the a11y check in interactive mode * add a little headline to the a11y log * updates based on feedback
1 parent fe3d898 commit 29343e8

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

packages/app/cypress/component/support/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { createPinia } from '../../../src/store'
2424
import { setActivePinia } from 'pinia'
2525
import type { Pinia } from 'pinia'
2626
import 'cypress-real-events/support'
27+
2728
import { installCustomPercyCommand } from '@packages/ui-components/cypress/support/customPercyCommand'
2829

2930
let pinia: Pinia

packages/frontend-shared/cypress/e2e/support/e2eSupport.ts

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type { SinonStub } from 'sinon'
1515
import type sinon from 'sinon'
1616
import type pDefer from 'p-defer'
1717
import 'cypress-plugin-tab'
18-
import 'cypress-axe'
1918
import type { Response } from 'cross-fetch'
2019

2120
configure({ testIdAttribute: 'data-cy' })

packages/frontend-shared/cypress/support/component.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,12 @@ Cypress.on('uncaught:exception', (err) => !err.message.includes('ResizeObserver
7676

7777
registerMountFn()
7878
addVueCommand()
79-
installCustomPercyCommand()
79+
installCustomPercyCommand({
80+
elementOverrides: {
81+
'svg.animate-spin': ($el) => {
82+
$el.attr('style', 'animation: none !important')
83+
},
84+
},
85+
})
86+
8087
addNetworkCommands()

packages/ui-components/cypress/support/customPercyCommand.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import '@percy/cypress'
44
import type { SnapshotOptions } from '@percy/core'
5+
import 'cypress-axe'
56

67
export interface CustomSnapshotOptions extends SnapshotOptions{
78
/**
@@ -54,6 +55,54 @@ declare global {
5455
}
5556
}
5657

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+
57106
class ElementOverrideManager {
58107
private mutationStack: Array<MutationRecord> | undefined = undefined
59108

@@ -189,7 +238,7 @@ const applySnapshotMutations = ({
189238
})
190239
}
191240

192-
export const installCustomPercyCommand = ({ before, elementOverrides }: {before?: () => void, elementOverrides?: CustomSnapshotOptions['elementOverrides'] } = {}) => {
241+
export const installCustomPercyCommand = ({ before, elementOverrides }: {before?: () => void, elementOverrides?: CustomSnapshotOptions['elementOverrides'], isComponentTesting?: boolean } = {}) => {
193242
/**
194243
* A custom Percy command that allows for additional mutations prior to snapshot generation. Mutations will be
195244
* 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?
239288
// that is representative of the snapshot that would be taken by Percy and can be
240289
// used for validation during development.
241290
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+
242296
return applySnapshotMutations({
243297
...snapshotMutationOptions,
244298
log: 'percy: skipping snapshot in interactive mode',

0 commit comments

Comments
 (0)