internalReporter.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. });
  33. module.exports = __toCommonJS(internalReporter_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_utils = require("playwright-core/lib/utils");
  36. var import_base = require("./base");
  37. var import_multiplexer = require("./multiplexer");
  38. var import_test = require("../common/test");
  39. var import_babelBundle = require("../transform/babelBundle");
  40. var import_reporterV2 = require("./reporterV2");
  41. class InternalReporter {
  42. constructor(reporters) {
  43. this._didBegin = false;
  44. this._reporter = new import_multiplexer.Multiplexer(reporters.map(import_reporterV2.wrapReporterAsV2));
  45. }
  46. version() {
  47. return "v2";
  48. }
  49. onConfigure(config) {
  50. this._config = config;
  51. this._startTime = /* @__PURE__ */ new Date();
  52. this._monotonicStartTime = (0, import_utils.monotonicTime)();
  53. this._reporter.onConfigure?.(config);
  54. }
  55. onBegin(suite) {
  56. this._didBegin = true;
  57. this._reporter.onBegin?.(suite);
  58. }
  59. onTestBegin(test, result) {
  60. this._reporter.onTestBegin?.(test, result);
  61. }
  62. onStdOut(chunk, test, result) {
  63. this._reporter.onStdOut?.(chunk, test, result);
  64. }
  65. onStdErr(chunk, test, result) {
  66. this._reporter.onStdErr?.(chunk, test, result);
  67. }
  68. onTestEnd(test, result) {
  69. this._addSnippetToTestErrors(test, result);
  70. this._reporter.onTestEnd?.(test, result);
  71. }
  72. async onEnd(result) {
  73. if (!this._didBegin) {
  74. this.onBegin(new import_test.Suite("", "root"));
  75. }
  76. return await this._reporter.onEnd?.({
  77. ...result,
  78. startTime: this._startTime,
  79. duration: (0, import_utils.monotonicTime)() - this._monotonicStartTime
  80. });
  81. }
  82. async onExit() {
  83. await this._reporter.onExit?.();
  84. }
  85. onError(error) {
  86. addLocationAndSnippetToError(this._config, error);
  87. this._reporter.onError?.(error);
  88. }
  89. onStepBegin(test, result, step) {
  90. this._reporter.onStepBegin?.(test, result, step);
  91. }
  92. onStepEnd(test, result, step) {
  93. this._addSnippetToStepError(test, step);
  94. this._reporter.onStepEnd?.(test, result, step);
  95. }
  96. printsToStdio() {
  97. return this._reporter.printsToStdio ? this._reporter.printsToStdio() : true;
  98. }
  99. _addSnippetToTestErrors(test, result) {
  100. for (const error of result.errors)
  101. addLocationAndSnippetToError(this._config, error, test.location.file);
  102. }
  103. _addSnippetToStepError(test, step) {
  104. if (step.error)
  105. addLocationAndSnippetToError(this._config, step.error, test.location.file);
  106. }
  107. }
  108. function addLocationAndSnippetToError(config, error, file) {
  109. if (error.stack && !error.location)
  110. error.location = (0, import_base.prepareErrorStack)(error.stack).location;
  111. const location = error.location;
  112. if (!location)
  113. return;
  114. try {
  115. const tokens = [];
  116. const source = import_fs.default.readFileSync(location.file, "utf8");
  117. const codeFrame = (0, import_babelBundle.codeFrameColumns)(source, { start: location }, { highlightCode: true });
  118. if (!file || import_fs.default.realpathSync(file) !== location.file) {
  119. tokens.push(import_base.internalScreen.colors.gray(` at `) + `${(0, import_base.relativeFilePath)(import_base.internalScreen, config, location.file)}:${location.line}`);
  120. tokens.push("");
  121. }
  122. tokens.push(codeFrame);
  123. error.snippet = tokens.join("\n");
  124. } catch (e) {
  125. }
  126. }
  127. // Annotate the CommonJS export names for ESM import in node:
  128. 0 && (module.exports = {
  129. InternalReporter
  130. });