@sa11y/jest 5.1.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,6 +19,7 @@ Accessibility matcher for [Jest](https://jestjs.io)
19
19
  - [JSON result transformation](#json-result-transformation)
20
20
  - [Limitations](#limitations)
21
21
  - [Disabled Checks](#disabled-checks)
22
+ - [Jest toMatchSnapshot() API Wrapper](#jest-tomatchsnapshot-api-wrapper)
22
23
 
23
24
  <!-- END doctoc generated TOC please keep comment here to allow auto update -->
24
25
 
@@ -278,3 +279,7 @@ Automatic checks currently has the following limitations.
278
279
  ### Disabled Checks
279
280
 
280
281
  - @sa11y/jest automatic checks also disabled rules which were disabled in `toBeAccessible` Jest API ([disabled-checks](https://github.com/salesforce/sa11y/tree/master/packages/jest#disabled-checks))
282
+
283
+ ### Jest toMatchSnapshot() API Wrapper
284
+
285
+ - When Snapshot testing is done, jest's `toMatchSnapshot()` API is altering the document.body element attributes as needed to generate snapshots thereby the a11y checks which happen later on `document.body` isn't running on actual elements. To solve this, a wrapper has been introduced to restore the document.body DOM before jest's `toMatchSnapshot` is invoked and use the restored version of document.body for a11y checks.
@@ -7,6 +7,7 @@ export type AutoCheckOpts = {
7
7
  consolidateResults?: boolean;
8
8
  filesFilter?: string[];
9
9
  };
10
+ export declare const setOriginalDocumentBodyHtml: (bodyHtml: string | null) => void;
10
11
  /**
11
12
  * Check if current test file needs to be skipped based on any provided filter
12
13
  */
@@ -1 +1 @@
1
- {"version":3,"file":"automatic.d.ts","sourceRoot":"","sources":["../src/automatic.ts"],"names":[],"mappings":"AAeA;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAI7B,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B,CAAC;AAYF;;GAEG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAUtF;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAAC,IAAI,GAAE,aAAoC,GAAG,OAAO,CAAC,IAAI,CAAC,CAgC9F;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,GAAE,aAAoC,GAAG,IAAI,CAM7F"}
1
+ {"version":3,"file":"automatic.d.ts","sourceRoot":"","sources":["../src/automatic.ts"],"names":[],"mappings":"AAeA;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAI7B,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B,CAAC;AAcF,eAAO,MAAM,2BAA2B,aAAc,MAAM,GAAG,IAAI,SAElE,CAAC;AAEF;;GAEG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAUtF;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAAC,IAAI,GAAE,aAAoC,GAAG,OAAO,CAAC,IAAI,CAAC,CAoC9F;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,GAAE,aAAoC,GAAG,IAAI,CAM7F"}
package/dist/automatic.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.registerSa11yAutomaticChecks = exports.automaticCheck = exports.skipTest = void 0;
9
+ exports.registerSa11yAutomaticChecks = exports.automaticCheck = exports.skipTest = exports.setOriginalDocumentBodyHtml = void 0;
10
10
  const common_1 = require("@sa11y/common");
11
11
  const assert_1 = require("@sa11y/assert");
12
12
  const format_1 = require("@sa11y/format");
@@ -23,6 +23,11 @@ const defaultAutoCheckOpts = {
23
23
  consolidateResults: true,
24
24
  filesFilter: [],
25
25
  };
26
+ let originalDocumentBodyHtml = null;
27
+ const setOriginalDocumentBodyHtml = (bodyHtml) => {
28
+ originalDocumentBodyHtml = bodyHtml ?? null;
29
+ };
30
+ exports.setOriginalDocumentBodyHtml = setOriginalDocumentBodyHtml;
26
31
  /**
27
32
  * Check if current test file needs to be skipped based on any provided filter
28
33
  */
@@ -49,6 +54,9 @@ async function automaticCheck(opts = defaultAutoCheckOpts) {
49
54
  return;
50
55
  }
51
56
  const violations = [];
57
+ if (originalDocumentBodyHtml) {
58
+ document.body.innerHTML = originalDocumentBodyHtml;
59
+ }
52
60
  // Create a DOM walker filtering only elements (skipping text, comment nodes etc)
53
61
  const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);
54
62
  let currNode = walker.firstChild();
@@ -66,6 +74,7 @@ async function automaticCheck(opts = defaultAutoCheckOpts) {
66
74
  }
67
75
  }
68
76
  finally {
77
+ (0, exports.setOriginalDocumentBodyHtml)(null);
69
78
  if (opts.cleanupAfterEach)
70
79
  document.body.innerHTML = ''; // remove non-element nodes
71
80
  // TODO (spike): Disable stack trace for automatic checks.
@@ -1 +1 @@
1
- {"version":3,"file":"groupViolationResultsProcessor.d.ts","sourceRoot":"","sources":["../src/groupViolationResultsProcessor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAA+B,MAAM,mBAAmB,CAAC;AAkHlF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,GAAG,gBAAgB,CAapF"}
1
+ {"version":3,"file":"groupViolationResultsProcessor.d.ts","sourceRoot":"","sources":["../src/groupViolationResultsProcessor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAA+B,MAAM,mBAAmB,CAAC;AAqHlF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,GAAG,gBAAgB,CAapF"}
@@ -20,6 +20,7 @@ function createA11yErrorElements(errorElements) {
20
20
  function createA11yRuleViolation(a11yRule, ruleIndex) {
21
21
  return `(${ruleIndex}) [${a11yRule.id}] ${a11yRule.description}
22
22
  * Help URL: ${a11yRule.helpUrl}
23
+ * WCAG Criteria: ${a11yRule.wcagCriteria}
23
24
  * More info: ${a11yRule.summary}
24
25
  * Error element(s) : ${a11yRule.errorElements.length}
25
26
  ${createA11yErrorElements(a11yRule.errorElements)}`;
@@ -53,6 +54,7 @@ function processA11yErrors(results, testSuite, testResult) {
53
54
  id: a11yResult.id,
54
55
  description: a11yResult.description,
55
56
  helpUrl: a11yResult.helpUrl,
57
+ wcagCriteria: a11yResult.wcag,
56
58
  summary: a11yResult.summary,
57
59
  errorElements: [],
58
60
  };
@@ -1 +1 @@
1
- {"version":3,"file":"resultsProcessor.d.ts","sourceRoot":"","sources":["../src/resultsProcessor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAAiE,MAAM,mBAAmB,CAAC;AAuFpH;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,GAAG,gBAAgB,CAyBpF"}
1
+ {"version":3,"file":"resultsProcessor.d.ts","sourceRoot":"","sources":["../src/resultsProcessor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAAiE,MAAM,mBAAmB,CAAC;AAwFpH;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,GAAG,gBAAgB,CAyBpF"}
@@ -27,6 +27,7 @@ function createA11yTestResult(testResult, a11yResult) {
27
27
  failureMessages: [
28
28
  `${common_1.errMsgHeader}: ${a11yResult.description}
29
29
  Help: ${a11yResult.helpUrl}
30
+ WCAG Criteria: ${a11yResult.wcag}
30
31
  CSS Selectors: ${a11yResult.selectors}
31
32
  HTML element: ${a11yResult.html}
32
33
  Tests: "${testResult.fullName}"
@@ -1 +1 @@
1
- {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAgC,MAAM,aAAa,CAAC;AAG1E,eAAO,MAAM,aAAa,UAiBzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG;IACpB,aAAa,EAAE,aAAa,CAAC;CAKhC,CAAC;AAaF;;;GAGG;AACH,wBAAgB,KAAK,CAAC,IAAI,GAAE,SAA4B,GAAG,IAAI,CAW9D;AAED;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAU3C;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,WAAW,WAAgB,GAAG,UAAU,CAK3F"}
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,aAAa,EAA6D,MAAM,aAAa,CAAC;AASvG,eAAO,MAAM,aAAa,UAiBzB,CAAC;AAWF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG;IACpB,aAAa,EAAE,aAAa,CAAC;CAKhC,CAAC;AAaF;;;GAGG;AACH,wBAAgB,KAAK,CAAC,IAAI,GAAE,SAA4B,GAAG,IAAI,CAW9D;AAED;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAW3C;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,WAAW,WAAgB,GAAG,UAAU,CAK3F"}
package/dist/setup.js CHANGED
@@ -10,6 +10,7 @@ exports.adaptA11yConfig = exports.registerSa11yMatcher = exports.setup = exports
10
10
  const matcher_1 = require("./matcher");
11
11
  const automatic_1 = require("./automatic");
12
12
  const globals_1 = require("@jest/globals");
13
+ const jest_snapshot_1 = require("jest-snapshot");
13
14
  exports.disabledRules = [
14
15
  // Descendancy checks that would fail at unit/component level, but pass at page level
15
16
  'aria-required-children',
@@ -28,6 +29,14 @@ exports.disabledRules = [
28
29
  'audio-caption',
29
30
  'video-caption',
30
31
  ];
32
+ function wrapperSnapshotMatcher(originalMatcher) {
33
+ return function (...args) {
34
+ (0, automatic_1.setOriginalDocumentBodyHtml)(document?.body?.innerHTML ?? '');
35
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
36
+ // @ts-ignore
37
+ return originalMatcher.call(globals_1.expect.getState(), ...args);
38
+ };
39
+ }
31
40
  /**
32
41
  * Default options for sa11y jest matcher
33
42
  */
@@ -59,8 +68,9 @@ exports.setup = setup;
59
68
  * Register accessibility helpers toBeAccessible as jest matchers
60
69
  */
61
70
  function registerSa11yMatcher() {
71
+ const wrapper = wrapperSnapshotMatcher(jest_snapshot_1.toMatchSnapshot);
62
72
  if (globals_1.expect !== undefined) {
63
- globals_1.expect.extend({ toBeAccessible: matcher_1.toBeAccessible });
73
+ globals_1.expect.extend({ toBeAccessible: matcher_1.toBeAccessible, toMatchSnapshot: wrapper });
64
74
  }
65
75
  else {
66
76
  throw new Error("Unable to find Jest's expect function." +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sa11y/jest",
3
- "version": "5.1.0",
3
+ "version": "5.3.0",
4
4
  "description": "Accessibility testing matcher for Jest",
5
5
  "license": "BSD-3-Clause",
6
6
  "homepage": "https://github.com/salesforce/sa11y/tree/master/packages/jest#readme",
@@ -24,9 +24,9 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "@jest/test-result": "^27",
27
- "@sa11y/assert": "5.1.0",
28
- "@sa11y/format": "5.1.0",
29
- "@sa11y/preset-rules": "5.1.0",
27
+ "@sa11y/assert": "5.3.0",
28
+ "@sa11y/format": "5.3.0",
29
+ "@sa11y/preset-rules": "5.3.0",
30
30
  "jest-matcher-utils": "^27"
31
31
  },
32
32
  "peerDependencies": {
@@ -34,8 +34,10 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@jest/globals": "28.1.3",
37
- "@sa11y/common": "5.1.0",
38
- "@sa11y/test-utils": "5.1.0"
37
+ "@sa11y/common": "5.3.0",
38
+ "@sa11y/test-utils": "5.3.0",
39
+ "expect": "28.1.3",
40
+ "jest-snapshot": "28.1.3"
39
41
  },
40
42
  "publishConfig": {
41
43
  "access": "public"
@@ -43,5 +45,5 @@
43
45
  "engines": {
44
46
  "node": "^16 || ^18"
45
47
  },
46
- "gitHead": "60b921be53c4ad776944a6538d429e93b127bf29"
48
+ "gitHead": "8549c87c4b3dbfe49ed5aed972e0ecea9d4f9ad8"
47
49
  }