toMatchText.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var toMatchText_exports = {};
  20. __export(toMatchText_exports, {
  21. toMatchText: () => toMatchText
  22. });
  23. module.exports = __toCommonJS(toMatchText_exports);
  24. var import_utils = require("playwright-core/lib/utils");
  25. var import_util = require("../util");
  26. var import_expect = require("./expect");
  27. var import_matcherHint = require("./matcherHint");
  28. var import_expectBundle = require("../common/expectBundle");
  29. async function toMatchText(matcherName, receiver, receiverType, query, expected, options = {}) {
  30. (0, import_util.expectTypes)(receiver, [receiverType], matcherName);
  31. const matcherOptions = {
  32. isNot: this.isNot,
  33. promise: this.promise
  34. };
  35. if (!(typeof expected === "string") && !(expected && typeof expected.test === "function")) {
  36. throw new Error([
  37. (0, import_matcherHint.matcherHint)(this, receiverType === "Locator" ? receiver : void 0, matcherName, options.receiverLabel ?? receiver, expected, matcherOptions, void 0, void 0, true),
  38. `${import_utils.colors.bold("Matcher error")}: ${(0, import_expectBundle.EXPECTED_COLOR)("expected")} value must be a string or regular expression`,
  39. this.utils.printWithType("Expected", expected, this.utils.printExpected)
  40. ].join("\n\n"));
  41. }
  42. const timeout = options.timeout ?? this.timeout;
  43. const { matches: pass, received, log, timedOut } = await query(!!this.isNot, timeout);
  44. if (pass === !this.isNot) {
  45. return {
  46. name: matcherName,
  47. message: () => "",
  48. pass,
  49. expected
  50. };
  51. }
  52. const stringSubstring = options.matchSubstring ? "substring" : "string";
  53. const receivedString = received || "";
  54. const notFound = received === import_matcherHint.kNoElementsFoundError;
  55. let printedReceived;
  56. let printedExpected;
  57. let printedDiff;
  58. if (pass) {
  59. if (typeof expected === "string") {
  60. if (notFound) {
  61. printedExpected = `Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}`;
  62. printedReceived = `Received: ${received}`;
  63. } else {
  64. printedExpected = `Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}`;
  65. const formattedReceived = (0, import_expect.printReceivedStringContainExpectedSubstring)(receivedString, receivedString.indexOf(expected), expected.length);
  66. printedReceived = `Received string: ${formattedReceived}`;
  67. }
  68. } else {
  69. if (notFound) {
  70. printedExpected = `Expected pattern: not ${this.utils.printExpected(expected)}`;
  71. printedReceived = `Received: ${received}`;
  72. } else {
  73. printedExpected = `Expected pattern: not ${this.utils.printExpected(expected)}`;
  74. const formattedReceived = (0, import_expect.printReceivedStringContainExpectedResult)(receivedString, typeof expected.exec === "function" ? expected.exec(receivedString) : null);
  75. printedReceived = `Received string: ${formattedReceived}`;
  76. }
  77. }
  78. } else {
  79. const labelExpected = `Expected ${typeof expected === "string" ? stringSubstring : "pattern"}`;
  80. if (notFound) {
  81. printedExpected = `${labelExpected}: ${this.utils.printExpected(expected)}`;
  82. printedReceived = `Received: ${received}`;
  83. } else {
  84. printedDiff = this.utils.printDiffOrStringify(expected, receivedString, labelExpected, "Received string", false);
  85. }
  86. }
  87. const message = () => {
  88. const resultDetails = printedDiff ? printedDiff : printedExpected + "\n" + printedReceived;
  89. const hints = (0, import_matcherHint.matcherHint)(this, receiverType === "Locator" ? receiver : void 0, matcherName, options.receiverLabel ?? "locator", void 0, matcherOptions, timedOut ? timeout : void 0, resultDetails, true);
  90. return hints + (0, import_util.callLogText)(log);
  91. };
  92. return {
  93. name: matcherName,
  94. expected,
  95. message,
  96. pass,
  97. actual: received,
  98. log,
  99. timeout: timedOut ? timeout : void 0
  100. };
  101. }
  102. // Annotate the CommonJS export names for ESM import in node:
  103. 0 && (module.exports = {
  104. toMatchText
  105. });