toMatchText.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_util = require("../util");
  25. var import_expect = require("./expect");
  26. var import_matcherHint = require("./matcherHint");
  27. var import_expectBundle = require("../common/expectBundle");
  28. async function toMatchText(matcherName, receiver, receiverType, query, expected, options = {}) {
  29. (0, import_util.expectTypes)(receiver, [receiverType], matcherName);
  30. const locator = receiverType === "Locator" ? receiver : void 0;
  31. if (!(typeof expected === "string") && !(expected && typeof expected.test === "function")) {
  32. const errorMessage2 = `Error: ${(0, import_expectBundle.EXPECTED_COLOR)("expected")} value must be a string or regular expression
  33. ${this.utils.printWithType("Expected", expected, this.utils.printExpected)}`;
  34. throw new Error((0, import_matcherHint.formatMatcherMessage)(this, { locator, matcherName, expectation: "expected", errorMessage: errorMessage2 }));
  35. }
  36. const timeout = options.timeout ?? this.timeout;
  37. const { matches: pass, received, log, timedOut, errorMessage } = await query(!!this.isNot, timeout);
  38. if (pass === !this.isNot) {
  39. return {
  40. name: matcherName,
  41. message: () => "",
  42. pass,
  43. expected
  44. };
  45. }
  46. const expectedSuffix = typeof expected === "string" ? options.matchSubstring ? " substring" : "" : " pattern";
  47. const receivedSuffix = typeof expected === "string" ? options.matchSubstring ? " string" : "" : " string";
  48. const receivedString = received || "";
  49. let printedReceived;
  50. let printedExpected;
  51. let printedDiff;
  52. if (pass) {
  53. if (typeof expected === "string") {
  54. printedExpected = `Expected${expectedSuffix}: not ${this.utils.printExpected(expected)}`;
  55. if (!errorMessage) {
  56. const formattedReceived = (0, import_expect.printReceivedStringContainExpectedSubstring)(receivedString, receivedString.indexOf(expected), expected.length);
  57. printedReceived = `Received${receivedSuffix}: ${formattedReceived}`;
  58. }
  59. } else {
  60. printedExpected = `Expected${expectedSuffix}: not ${this.utils.printExpected(expected)}`;
  61. if (!errorMessage) {
  62. const formattedReceived = (0, import_expect.printReceivedStringContainExpectedResult)(receivedString, typeof expected.exec === "function" ? expected.exec(receivedString) : null);
  63. printedReceived = `Received${receivedSuffix}: ${formattedReceived}`;
  64. }
  65. }
  66. } else {
  67. if (errorMessage)
  68. printedExpected = `Expected${expectedSuffix}: ${this.utils.printExpected(expected)}`;
  69. else
  70. printedDiff = this.utils.printDiffOrStringify(expected, receivedString, `Expected${expectedSuffix}`, `Received${receivedSuffix}`, false);
  71. }
  72. const message = () => {
  73. return (0, import_matcherHint.formatMatcherMessage)(this, {
  74. matcherName,
  75. expectation: "expected",
  76. locator,
  77. timeout,
  78. timedOut,
  79. printedExpected,
  80. printedReceived,
  81. printedDiff,
  82. log,
  83. errorMessage
  84. });
  85. };
  86. return {
  87. name: matcherName,
  88. expected,
  89. message,
  90. pass,
  91. actual: received,
  92. log,
  93. timeout: timedOut ? timeout : void 0
  94. };
  95. }
  96. // Annotate the CommonJS export names for ESM import in node:
  97. 0 && (module.exports = {
  98. toMatchText
  99. });