internalReporter.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var internalReporter_exports = {};
  30. __export(internalReporter_exports, {
  31. InternalReporter: () => InternalReporter,
  32. addLocationAndSnippetToError: () => addLocationAndSnippetToError
  33. });
  34. module.exports = __toCommonJS(internalReporter_exports);
  35. var import_fs = __toESM(require("fs"));
  36. var import_utils = require("playwright-core/lib/utils");
  37. var import_base = require("./base");
  38. var import_multiplexer = require("./multiplexer");
  39. var import_test = require("../common/test");
  40. var import_babelBundle = require("../transform/babelBundle");
  41. var import_reporterV2 = require("./reporterV2");
  42. class InternalReporter {
  43. constructor(reporters) {
  44. this._didBegin = false;
  45. this._reporter = new import_multiplexer.Multiplexer(reporters.map(import_reporterV2.wrapReporterAsV2));
  46. }
  47. version() {
  48. return "v2";
  49. }
  50. onConfigure(config) {
  51. this._config = config;
  52. this._startTime = /* @__PURE__ */ new Date();
  53. this._monotonicStartTime = (0, import_utils.monotonicTime)();
  54. this._reporter.onConfigure?.(config);
  55. }
  56. onBegin(suite) {
  57. this._didBegin = true;
  58. this._reporter.onBegin?.(suite);
  59. }
  60. onTestBegin(test, result) {
  61. this._reporter.onTestBegin?.(test, result);
  62. }
  63. onStdOut(chunk, test, result) {
  64. this._reporter.onStdOut?.(chunk, test, result);
  65. }
  66. onStdErr(chunk, test, result) {
  67. this._reporter.onStdErr?.(chunk, test, result);
  68. }
  69. onTestEnd(test, result) {
  70. this._addSnippetToTestErrors(test, result);
  71. this._reporter.onTestEnd?.(test, result);
  72. }
  73. async onEnd(result) {
  74. if (!this._didBegin) {
  75. this.onBegin(new import_test.Suite("", "root"));
  76. }
  77. return await this._reporter.onEnd?.({
  78. ...result,
  79. startTime: this._startTime,
  80. duration: (0, import_utils.monotonicTime)() - this._monotonicStartTime
  81. });
  82. }
  83. async onExit() {
  84. await this._reporter.onExit?.();
  85. }
  86. onError(error) {
  87. addLocationAndSnippetToError(this._config, error);
  88. this._reporter.onError?.(error);
  89. }
  90. onStepBegin(test, result, step) {
  91. this._reporter.onStepBegin?.(test, result, step);
  92. }
  93. onStepEnd(test, result, step) {
  94. this._addSnippetToStepError(test, step);
  95. this._reporter.onStepEnd?.(test, result, step);
  96. }
  97. printsToStdio() {
  98. return this._reporter.printsToStdio ? this._reporter.printsToStdio() : true;
  99. }
  100. _addSnippetToTestErrors(test, result) {
  101. for (const error of result.errors)
  102. addLocationAndSnippetToError(this._config, error, test.location.file);
  103. }
  104. _addSnippetToStepError(test, step) {
  105. if (step.error)
  106. addLocationAndSnippetToError(this._config, step.error, test.location.file);
  107. }
  108. }
  109. function addLocationAndSnippetToError(config, error, file) {
  110. if (error.stack && !error.location)
  111. error.location = (0, import_base.prepareErrorStack)(error.stack).location;
  112. const location = error.location;
  113. if (!location)
  114. return;
  115. try {
  116. const tokens = [];
  117. const source = import_fs.default.readFileSync(location.file, "utf8");
  118. const codeFrame = (0, import_babelBundle.codeFrameColumns)(source, { start: location }, { highlightCode: true });
  119. if (!file || import_fs.default.realpathSync(file) !== location.file) {
  120. tokens.push(import_base.internalScreen.colors.gray(` at `) + `${(0, import_base.relativeFilePath)(import_base.internalScreen, config, location.file)}:${location.line}`);
  121. tokens.push("");
  122. }
  123. tokens.push(codeFrame);
  124. error.snippet = tokens.join("\n");
  125. } catch (e) {
  126. }
  127. }
  128. // Annotate the CommonJS export names for ESM import in node:
  129. 0 && (module.exports = {
  130. InternalReporter,
  131. addLocationAndSnippetToError
  132. });