toEqual.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, receiver, receiverType, query, expected, options = {}, messagePreventExtraStatIndent) {
  30. (0, import_util.expectTypes)(receiver, [receiverType], matcherName);
  31. const matcherOptions = {
  32. comment: options.contains ? "" : "deep equality",
  33. isNot: this.isNot,
  34. promise: this.promise
  35. };
  36. const timeout = options.timeout ?? this.timeout;
  37. const { matches: pass, received, log, timedOut } = await query(!!this.isNot, timeout);
  38. if (pass === !this.isNot) {
  39. return {
  40. name: matcherName,
  41. message: () => "",
  42. pass,
  43. expected
  44. };
  45. }
  46. let printedReceived;
  47. let printedExpected;
  48. let printedDiff;
  49. if (pass) {
  50. printedExpected = `Expected: not ${this.utils.printExpected(expected)}`;
  51. printedReceived = `Received: ${this.utils.printReceived(received)}`;
  52. } else if (Array.isArray(expected) && Array.isArray(received)) {
  53. const normalizedExpected = expected.map((exp, index) => {
  54. const rec = received[index];
  55. if ((0, import_utils.isRegExp)(exp))
  56. return exp.test(rec) ? rec : exp;
  57. return exp;
  58. });
  59. printedDiff = this.utils.printDiffOrStringify(
  60. normalizedExpected,
  61. received,
  62. EXPECTED_LABEL,
  63. RECEIVED_LABEL,
  64. false
  65. );
  66. } else {
  67. printedDiff = this.utils.printDiffOrStringify(
  68. expected,
  69. received,
  70. EXPECTED_LABEL,
  71. RECEIVED_LABEL,
  72. false
  73. );
  74. }
  75. const message = () => {
  76. const details = printedDiff || `${printedExpected}
  77. ${printedReceived}`;
  78. const header = (0, import_matcherHint.matcherHint)(this, receiver, matcherName, "locator", void 0, matcherOptions, timedOut ? timeout : void 0, details, messagePreventExtraStatIndent);
  79. return `${header}${(0, import_util.callLogText)(log)}`;
  80. };
  81. return {
  82. actual: received,
  83. expected,
  84. message,
  85. name: matcherName,
  86. pass,
  87. log,
  88. timeout: timedOut ? timeout : void 0
  89. };
  90. }
  91. // Annotate the CommonJS export names for ESM import in node:
  92. 0 && (module.exports = {
  93. toEqual
  94. });