toMatchAriaSnapshot.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_util = require("../util");
  39. var import_expect = require("./expect");
  40. var import_globals = require("../common/globals");
  41. async function toMatchAriaSnapshot(locator, expectedParam, options = {}) {
  42. const matcherName = "toMatchAriaSnapshot";
  43. const testInfo = (0, import_globals.currentTestInfo)();
  44. if (!testInfo)
  45. throw new Error(`toMatchAriaSnapshot() must be called during the test`);
  46. if (testInfo._projectInternal.ignoreSnapshots)
  47. return { pass: !this.isNot, message: () => "", name: "toMatchAriaSnapshot", expected: "" };
  48. const updateSnapshots = testInfo.config.updateSnapshots;
  49. let expected;
  50. let timeout;
  51. let expectedPath;
  52. if ((0, import_utils.isString)(expectedParam)) {
  53. expected = expectedParam;
  54. timeout = options.timeout ?? this.timeout;
  55. } else {
  56. const legacyPath = testInfo._resolveSnapshotPaths("aria", expectedParam?.name, "dontUpdateSnapshotIndex", ".yml").absoluteSnapshotPath;
  57. expectedPath = testInfo._resolveSnapshotPaths("aria", expectedParam?.name, "updateSnapshotIndex").absoluteSnapshotPath;
  58. if (!await (0, import_util.fileExistsAsync)(expectedPath) && await (0, import_util.fileExistsAsync)(legacyPath))
  59. expectedPath = legacyPath;
  60. expected = await import_fs.default.promises.readFile(expectedPath, "utf8").catch(() => "");
  61. timeout = expectedParam?.timeout ?? this.timeout;
  62. }
  63. const generateMissingBaseline = updateSnapshots === "missing" && !expected;
  64. if (generateMissingBaseline) {
  65. if (this.isNot) {
  66. const message2 = `Matchers using ".not" can't generate new baselines`;
  67. return { pass: this.isNot, message: () => message2, name: "toMatchAriaSnapshot" };
  68. } else {
  69. expected = `- none "Generating new baseline"`;
  70. }
  71. }
  72. expected = unshift(expected);
  73. const { matches: pass, received, log, timedOut, errorMessage } = await locator._expect("to.match.aria", { expectedValue: expected, isNot: this.isNot, timeout });
  74. const typedReceived = received;
  75. const message = () => {
  76. let printedExpected;
  77. let printedReceived;
  78. let printedDiff;
  79. if (errorMessage) {
  80. printedExpected = `Expected: ${this.isNot ? "not " : ""}${this.utils.printExpected(expected)}`;
  81. } else if (pass) {
  82. const receivedString = (0, import_expect.printReceivedStringContainExpectedSubstring)(typedReceived.raw, typedReceived.raw.indexOf(expected), expected.length);
  83. printedExpected = `Expected: not ${this.utils.printExpected(expected)}`;
  84. printedReceived = `Received: ${receivedString}`;
  85. } else {
  86. printedDiff = this.utils.printDiffOrStringify(expected, typedReceived.raw, "Expected", "Received", false);
  87. }
  88. return (0, import_matcherHint.formatMatcherMessage)(this, {
  89. matcherName,
  90. expectation: "expected",
  91. locator,
  92. timeout,
  93. timedOut,
  94. printedExpected,
  95. printedReceived,
  96. printedDiff,
  97. errorMessage,
  98. log
  99. });
  100. };
  101. if (errorMessage)
  102. return { pass: this.isNot, message, name: "toMatchAriaSnapshot", expected };
  103. if (!this.isNot) {
  104. if (updateSnapshots === "all" || updateSnapshots === "changed" && pass === this.isNot || generateMissingBaseline) {
  105. if (expectedPath) {
  106. await import_fs.default.promises.mkdir(import_path.default.dirname(expectedPath), { recursive: true });
  107. await import_fs.default.promises.writeFile(expectedPath, typedReceived.regex, "utf8");
  108. const relativePath = import_path.default.relative(process.cwd(), expectedPath);
  109. if (updateSnapshots === "missing") {
  110. const message2 = `A snapshot doesn't exist at ${relativePath}, writing actual.`;
  111. testInfo._hasNonRetriableError = true;
  112. testInfo._failWithError(new Error(message2));
  113. } else {
  114. const message2 = `A snapshot is generated at ${relativePath}.`;
  115. console.log(message2);
  116. }
  117. return { pass: true, message: () => "", name: "toMatchAriaSnapshot" };
  118. } else {
  119. const suggestedRebaseline = `\`
  120. ${(0, import_utils.escapeTemplateString)(indent(typedReceived.regex, "{indent} "))}
  121. {indent}\``;
  122. if (updateSnapshots === "missing") {
  123. const message2 = "A snapshot is not provided, generating new baseline.";
  124. testInfo._hasNonRetriableError = true;
  125. testInfo._failWithError(new Error(message2));
  126. }
  127. return { pass: false, message: () => "", name: "toMatchAriaSnapshot", suggestedRebaseline };
  128. }
  129. }
  130. }
  131. return {
  132. name: matcherName,
  133. expected,
  134. message,
  135. pass,
  136. actual: received,
  137. log,
  138. timeout: timedOut ? timeout : void 0
  139. };
  140. }
  141. function unshift(snapshot) {
  142. const lines = snapshot.split("\n");
  143. let whitespacePrefixLength = 100;
  144. for (const line of lines) {
  145. if (!line.trim())
  146. continue;
  147. const match = line.match(/^(\s*)/);
  148. if (match && match[1].length < whitespacePrefixLength)
  149. whitespacePrefixLength = match[1].length;
  150. }
  151. return lines.filter((t) => t.trim()).map((line) => line.substring(whitespacePrefixLength)).join("\n");
  152. }
  153. function indent(snapshot, indent2) {
  154. return snapshot.split("\n").map((line) => indent2 + line).join("\n");
  155. }
  156. // Annotate the CommonJS export names for ESM import in node:
  157. 0 && (module.exports = {
  158. toMatchAriaSnapshot
  159. });