ScuttleBot

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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button