|
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_util = require("../util"); |
|
38
|
var import_globals = require("../common/globals"); |
|
39
|
async function toMatchAriaSnapshot(locator, expectedParam, options = {}) { |
|
40
|
const matcherName = "toMatchAriaSnapshot"; |
|
41
|
const testInfo = (0, import_globals.currentTestInfo)(); |
|
42
|
if (!testInfo) |
|
43
|
throw new Error(`toMatchAriaSnapshot() must be called during the test`); |
|
44
|
if (testInfo._projectInternal.ignoreSnapshots) |
|
45
|
return { pass: !this.isNot, message: () => "", name: "toMatchAriaSnapshot", expected: "" }; |
|
46
|
const updateSnapshots = testInfo.config.updateSnapshots; |
|
47
|
let expected; |
|
48
|
let timeout; |
|
49
|
let expectedPath; |
|
50
|
if ((0, import_utils.isString)(expectedParam)) { |
|
51
|
expected = expectedParam; |
|
52
|
timeout = options.timeout ?? this.timeout; |
|
53
|
} else { |
|
54
|
const legacyPath = testInfo._resolveSnapshotPaths("aria", expectedParam?.name, "dontUpdateSnapshotIndex", ".yml").absoluteSnapshotPath; |
|
55
|
expectedPath = testInfo._resolveSnapshotPaths("aria", expectedParam?.name, "updateSnapshotIndex").absoluteSnapshotPath; |
|
56
|
if (!await (0, import_util.fileExistsAsync)(expectedPath) && await (0, import_util.fileExistsAsync)(legacyPath)) |
|
57
|
expectedPath = legacyPath; |
|
58
|
expected = await import_fs.default.promises.readFile(expectedPath, "utf8").catch(() => ""); |
|
59
|
timeout = expectedParam?.timeout ?? this.timeout; |
|
60
|
} |
|
61
|
const generateMissingBaseline = updateSnapshots === "missing" && !expected; |
|
62
|
if (generateMissingBaseline) { |
|
63
|
if (this.isNot) { |
|
64
|
const message2 = `Matchers using ".not" can't generate new baselines`; |
|
65
|
return { pass: this.isNot, message: () => message2, name: "toMatchAriaSnapshot" }; |
|
66
|
} else { |
|
67
|
expected = `- none "Generating new baseline"`; |
|
68
|
} |
|
69
|
} |
|
70
|
expected = unshift(expected); |
|
71
|
const { matches: pass, received, log, timedOut, errorMessage } = await locator._expect("to.match.aria", { expectedValue: expected, isNot: this.isNot, timeout }); |
|
72
|
const typedReceived = received; |
|
73
|
const message = () => { |
|
74
|
let printedExpected; |
|
75
|
let printedReceived; |
|
76
|
let printedDiff; |
|
77
|
if (errorMessage) { |
|
78
|
printedExpected = `Expected: ${this.isNot ? "not " : ""}${this.utils.printExpected(expected)}`; |
|
79
|
} else if (pass) { |
|
80
|
const receivedString = (0, import_utils.printReceivedStringContainExpectedSubstring)(this.utils, typedReceived.raw, typedReceived.raw.indexOf(expected), expected.length); |
|
81
|
printedExpected = `Expected: not ${this.utils.printExpected(expected)}`; |
|
82
|
printedReceived = `Received: ${receivedString}`; |
|
83
|
} else { |
|
84
|
printedDiff = this.utils.printDiffOrStringify(expected, typedReceived.raw, "Expected", "Received", false); |
|
85
|
} |
|
86
|
return (0, import_utils.formatMatcherMessage)(this.utils, { |
|
87
|
isNot: this.isNot, |
|
88
|
promise: this.promise, |
|
89
|
matcherName, |
|
90
|
expectation: "expected", |
|
91
|
locator: locator.toString(), |
|
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
|
}); |
|
160
|
|