toEqual.js 3.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 toEqual_exports = {};
  20. __export(toEqual_exports, {
  21. toEqual: () => toEqual
  22. });
  23. module.exports = __toCommonJS(toEqual_exports);
  24. var import_utils = require("playwright-core/lib/utils");
  25. var import_util = require("../util");
  26. var import_matcherHint = require("./matcherHint");
  27. const EXPECTED_LABEL = "Expected";
  28. const RECEIVED_LABEL = "Received";
  29. async function toEqual(matcherName, locator, receiverType, query, expected, options = {}) {
  30. (0, import_util.expectTypes)(locator, [receiverType], matcherName);
  31. const timeout = options.timeout ?? this.timeout;
  32. const { matches: pass, received, log, timedOut, errorMessage } = await query(!!this.isNot, timeout);
  33. if (pass === !this.isNot) {
  34. return {
  35. name: matcherName,
  36. message: () => "",
  37. pass,
  38. expected
  39. };
  40. }
  41. let printedReceived;
  42. let printedExpected;
  43. let printedDiff;
  44. if (pass) {
  45. printedExpected = `Expected: not ${this.utils.printExpected(expected)}`;
  46. printedReceived = errorMessage ? "" : `Received: ${this.utils.printReceived(received)}`;
  47. } else if (errorMessage) {
  48. printedExpected = `Expected: ${this.utils.printExpected(expected)}`;
  49. } else if (Array.isArray(expected) && Array.isArray(received)) {
  50. const normalizedExpected = expected.map((exp, index) => {
  51. const rec = received[index];
  52. if ((0, import_utils.isRegExp)(exp))
  53. return exp.test(rec) ? rec : exp;
  54. return exp;
  55. });
  56. printedDiff = this.utils.printDiffOrStringify(
  57. normalizedExpected,
  58. received,
  59. EXPECTED_LABEL,
  60. RECEIVED_LABEL,
  61. false
  62. );
  63. } else {
  64. printedDiff = this.utils.printDiffOrStringify(
  65. expected,
  66. received,
  67. EXPECTED_LABEL,
  68. RECEIVED_LABEL,
  69. false
  70. );
  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. errorMessage,
  83. log
  84. });
  85. };
  86. return {
  87. actual: received,
  88. expected,
  89. message,
  90. name: matcherName,
  91. pass,
  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. toEqual
  99. });