toMatchAriaSnapshot.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var toMatchAriaSnapshot_exports = {};
  30. __export(toMatchAriaSnapshot_exports, {
  31. toMatchAriaSnapshot: () => toMatchAriaSnapshot
  32. });
  33. module.exports = __toCommonJS(toMatchAriaSnapshot_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_path = __toESM(require("path"));
  36. var import_utils = require("playwright-core/lib/utils");
  37. var import_matcherHint = require("./matcherHint");
  38. var import_expectBundle = require("../common/expectBundle");
  39. var import_util = require("../util");
  40. var import_expect = require("./expect");
  41. var import_globals = require("../common/globals");
  42. async function toMatchAriaSnapshot(receiver, expectedParam, options = {}) {
  43. const matcherName = "toMatchAriaSnapshot";
  44. const testInfo = (0, import_globals.currentTestInfo)();
  45. if (!testInfo)
  46. throw new Error(`toMatchAriaSnapshot() must be called during the test`);
  47. if (testInfo._projectInternal.ignoreSnapshots)
  48. return { pass: !this.isNot, message: () => "", name: "toMatchAriaSnapshot", expected: "" };
  49. const updateSnapshots = testInfo.config.updateSnapshots;
  50. const matcherOptions = {
  51. isNot: this.isNot,
  52. promise: this.promise
  53. };
  54. let expected;
  55. let timeout;
  56. let expectedPath;
  57. if ((0, import_utils.isString)(expectedParam)) {
  58. expected = expectedParam;
  59. timeout = options.timeout ?? this.timeout;
  60. } else {
  61. const legacyPath = testInfo._resolveSnapshotPaths("aria", expectedParam?.name, "dontUpdateSnapshotIndex", ".yml").absoluteSnapshotPath;
  62. expectedPath = testInfo._resolveSnapshotPaths("aria", expectedParam?.name, "updateSnapshotIndex").absoluteSnapshotPath;
  63. if (!await (0, import_util.fileExistsAsync)(expectedPath) && await (0, import_util.fileExistsAsync)(legacyPath))
  64. expectedPath = legacyPath;
  65. expected = await import_fs.default.promises.readFile(expectedPath, "utf8").catch(() => "");
  66. timeout = expectedParam?.timeout ?? this.timeout;
  67. }
  68. const generateMissingBaseline = updateSnapshots === "missing" && !expected;
  69. if (generateMissingBaseline) {
  70. if (this.isNot) {
  71. const message2 = `Matchers using ".not" can't generate new baselines`;
  72. return { pass: this.isNot, message: () => message2, name: "toMatchAriaSnapshot" };
  73. } else {
  74. expected = `- none "Generating new baseline"`;
  75. }
  76. }
  77. expected = unshift(expected);
  78. const { matches: pass, received, log, timedOut } = await receiver._expect("to.match.aria", { expectedValue: expected, isNot: this.isNot, timeout });
  79. const typedReceived = received;
  80. const matcherHintWithExpect = (expectedReceivedString) => {
  81. return (0, import_matcherHint.matcherHint)(this, receiver, matcherName, "locator", void 0, matcherOptions, timedOut ? timeout : void 0, expectedReceivedString);
  82. };
  83. const notFound = typedReceived === import_matcherHint.kNoElementsFoundError;
  84. if (notFound) {
  85. return {
  86. pass: this.isNot,
  87. message: () => matcherHintWithExpect(`Expected: ${this.utils.printExpected(expected)}
  88. Received: ${(0, import_expectBundle.EXPECTED_COLOR)("<element not found>")}`) + (0, import_util.callLogText)(log),
  89. name: "toMatchAriaSnapshot",
  90. expected
  91. };
  92. }
  93. const receivedText = typedReceived.raw;
  94. const message = () => {
  95. if (pass) {
  96. const receivedString = notFound ? receivedText : (0, import_expect.printReceivedStringContainExpectedSubstring)(receivedText, receivedText.indexOf(expected), expected.length);
  97. const expectedReceivedString = `Expected: not ${this.utils.printExpected(expected)}
  98. Received: ${receivedString}`;
  99. return matcherHintWithExpect(expectedReceivedString) + (0, import_util.callLogText)(log);
  100. } else {
  101. const labelExpected = `Expected`;
  102. const expectedReceivedString = notFound ? `${labelExpected}: ${this.utils.printExpected(expected)}
  103. Received: ${receivedText}` : this.utils.printDiffOrStringify(expected, receivedText, labelExpected, "Received", false);
  104. return matcherHintWithExpect(expectedReceivedString) + (0, import_util.callLogText)(log);
  105. }
  106. };
  107. if (!this.isNot) {
  108. if (updateSnapshots === "all" || updateSnapshots === "changed" && pass === this.isNot || generateMissingBaseline) {
  109. if (expectedPath) {
  110. await import_fs.default.promises.mkdir(import_path.default.dirname(expectedPath), { recursive: true });
  111. await import_fs.default.promises.writeFile(expectedPath, typedReceived.regex, "utf8");
  112. const relativePath = import_path.default.relative(process.cwd(), expectedPath);
  113. if (updateSnapshots === "missing") {
  114. const message2 = `A snapshot doesn't exist at ${relativePath}, writing actual.`;
  115. testInfo._hasNonRetriableError = true;
  116. testInfo._failWithError(new Error(message2));
  117. } else {
  118. const message2 = `A snapshot is generated at ${relativePath}.`;
  119. console.log(message2);
  120. }
  121. return { pass: true, message: () => "", name: "toMatchAriaSnapshot" };
  122. } else {
  123. const suggestedRebaseline = `\`
  124. ${(0, import_utils.escapeTemplateString)(indent(typedReceived.regex, "{indent} "))}
  125. {indent}\``;
  126. if (updateSnapshots === "missing") {
  127. const message2 = "A snapshot is not provided, generating new baseline.";
  128. testInfo._hasNonRetriableError = true;
  129. testInfo._failWithError(new Error(message2));
  130. }
  131. return { pass: false, message: () => "", name: "toMatchAriaSnapshot", suggestedRebaseline };
  132. }
  133. }
  134. }
  135. return {
  136. name: matcherName,
  137. expected,
  138. message,
  139. pass,
  140. actual: received,
  141. log,
  142. timeout: timedOut ? timeout : void 0
  143. };
  144. }
  145. function unshift(snapshot) {
  146. const lines = snapshot.split("\n");
  147. let whitespacePrefixLength = 100;
  148. for (const line of lines) {
  149. if (!line.trim())
  150. continue;
  151. const match = line.match(/^(\s*)/);
  152. if (match && match[1].length < whitespacePrefixLength)
  153. whitespacePrefixLength = match[1].length;
  154. }
  155. return lines.filter((t) => t.trim()).map((line) => line.substring(whitespacePrefixLength)).join("\n");
  156. }
  157. function indent(snapshot, indent2) {
  158. return snapshot.split("\n").map((line) => indent2 + line).join("\n");
  159. }
  160. // Annotate the CommonJS export names for ESM import in node:
  161. 0 && (module.exports = {
  162. toMatchAriaSnapshot
  163. });