internalReporter.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.InternalReporter = void 0;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _babelBundle = require("../transform/babelBundle");
  8. var _test = require("../common/test");
  9. var _base = require("./base");
  10. var _utils = require("playwright-core/lib/utils");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class InternalReporter {
  28. constructor(reporter) {
  29. this._reporter = void 0;
  30. this._didBegin = false;
  31. this._config = void 0;
  32. this._startTime = void 0;
  33. this._monotonicStartTime = void 0;
  34. this._reporter = reporter;
  35. }
  36. version() {
  37. return 'v2';
  38. }
  39. onConfigure(config) {
  40. this._config = config;
  41. this._startTime = new Date();
  42. this._monotonicStartTime = (0, _utils.monotonicTime)();
  43. this._reporter.onConfigure(config);
  44. }
  45. onBegin(suite) {
  46. this._didBegin = true;
  47. this._reporter.onBegin(suite);
  48. }
  49. onTestBegin(test, result) {
  50. this._reporter.onTestBegin(test, result);
  51. }
  52. onStdOut(chunk, test, result) {
  53. this._reporter.onStdOut(chunk, test, result);
  54. }
  55. onStdErr(chunk, test, result) {
  56. this._reporter.onStdErr(chunk, test, result);
  57. }
  58. onTestEnd(test, result) {
  59. this._addSnippetToTestErrors(test, result);
  60. this._reporter.onTestEnd(test, result);
  61. }
  62. async onEnd(result) {
  63. if (!this._didBegin) {
  64. // onBegin was not reported, emit it.
  65. this.onBegin(new _test.Suite('', 'root'));
  66. }
  67. return await this._reporter.onEnd({
  68. ...result,
  69. startTime: this._startTime,
  70. duration: (0, _utils.monotonicTime)() - this._monotonicStartTime
  71. });
  72. }
  73. async onExit() {
  74. await this._reporter.onExit();
  75. }
  76. onError(error) {
  77. addLocationAndSnippetToError(this._config, error);
  78. this._reporter.onError(error);
  79. }
  80. onStepBegin(test, result, step) {
  81. this._reporter.onStepBegin(test, result, step);
  82. }
  83. onStepEnd(test, result, step) {
  84. this._addSnippetToStepError(test, step);
  85. this._reporter.onStepEnd(test, result, step);
  86. }
  87. printsToStdio() {
  88. return this._reporter.printsToStdio();
  89. }
  90. _addSnippetToTestErrors(test, result) {
  91. for (const error of result.errors) addLocationAndSnippetToError(this._config, error, test.location.file);
  92. }
  93. _addSnippetToStepError(test, step) {
  94. if (step.error) addLocationAndSnippetToError(this._config, step.error, test.location.file);
  95. }
  96. }
  97. exports.InternalReporter = InternalReporter;
  98. function addLocationAndSnippetToError(config, error, file) {
  99. if (error.stack && !error.location) error.location = (0, _base.prepareErrorStack)(error.stack).location;
  100. const location = error.location;
  101. if (!location) return;
  102. try {
  103. const tokens = [];
  104. const source = _fs.default.readFileSync(location.file, 'utf8');
  105. const codeFrame = (0, _babelBundle.codeFrameColumns)(source, {
  106. start: location
  107. }, {
  108. highlightCode: true
  109. });
  110. // Convert /var/folders to /private/var/folders on Mac.
  111. if (!file || _fs.default.realpathSync(file) !== location.file) {
  112. tokens.push(_base.colors.gray(` at `) + `${(0, _base.relativeFilePath)(config, location.file)}:${location.line}`);
  113. tokens.push('');
  114. }
  115. tokens.push(codeFrame);
  116. error.snippet = tokens.join('\n');
  117. } catch (e) {
  118. // Failed to read the source file - that's ok.
  119. }
  120. }