matcherHint.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 matcherHint_exports = {};
  20. __export(matcherHint_exports, {
  21. ExpectError: () => ExpectError,
  22. callLogText: () => callLogText,
  23. formatMatcherMessage: () => formatMatcherMessage,
  24. isJestError: () => isJestError
  25. });
  26. module.exports = __toCommonJS(matcherHint_exports);
  27. var import_utils = require("playwright-core/lib/utils");
  28. var import_expectBundle = require("../common/expectBundle");
  29. function formatMatcherMessage(state, details) {
  30. const receiver = details.receiver ?? (details.locator ? "locator" : "page");
  31. let message = (0, import_expectBundle.DIM_COLOR)("expect(") + (0, import_expectBundle.RECEIVED_COLOR)(receiver) + (0, import_expectBundle.DIM_COLOR)(")" + (state.promise ? "." + state.promise : "") + (state.isNot ? ".not" : "") + ".") + details.matcherName + (0, import_expectBundle.DIM_COLOR)("(") + (0, import_expectBundle.EXPECTED_COLOR)(details.expectation) + (0, import_expectBundle.DIM_COLOR)(")") + " failed\n\n";
  32. const diffLines = details.printedDiff?.split("\n");
  33. if (diffLines?.length === 2) {
  34. details.printedExpected = diffLines[0];
  35. details.printedReceived = diffLines[1];
  36. details.printedDiff = void 0;
  37. }
  38. const align = !details.errorMessage && details.printedExpected?.startsWith("Expected:") && (!details.printedReceived || details.printedReceived.startsWith("Received:"));
  39. if (details.locator)
  40. message += `Locator: ${align ? " " : ""}${String(details.locator)}
  41. `;
  42. if (details.printedExpected)
  43. message += details.printedExpected + "\n";
  44. if (details.printedReceived)
  45. message += details.printedReceived + "\n";
  46. if (details.timedOut && details.timeout)
  47. message += `Timeout: ${align ? " " : ""}${details.timeout}ms
  48. `;
  49. if (details.printedDiff)
  50. message += details.printedDiff + "\n";
  51. if (details.errorMessage) {
  52. message += details.errorMessage;
  53. if (!details.errorMessage.endsWith("\n"))
  54. message += "\n";
  55. }
  56. message += callLogText(details.log);
  57. return message;
  58. }
  59. class ExpectError extends Error {
  60. constructor(jestError, customMessage, stackFrames) {
  61. super("");
  62. this.name = jestError.name;
  63. this.message = jestError.message;
  64. this.matcherResult = jestError.matcherResult;
  65. if (customMessage)
  66. this.message = customMessage + "\n\n" + this.message;
  67. this.stack = this.name + ": " + this.message + "\n" + (0, import_utils.stringifyStackFrames)(stackFrames).join("\n");
  68. }
  69. }
  70. function isJestError(e) {
  71. return e instanceof Error && "matcherResult" in e;
  72. }
  73. const callLogText = (log) => {
  74. if (!log || !log.some((l) => !!l))
  75. return "";
  76. return `
  77. Call log:
  78. ${(0, import_expectBundle.DIM_COLOR)(log.join("\n"))}
  79. `;
  80. };
  81. // Annotate the CommonJS export names for ESM import in node:
  82. 0 && (module.exports = {
  83. ExpectError,
  84. callLogText,
  85. formatMatcherMessage,
  86. isJestError
  87. });