reporterV2.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.wrapReporterAsV2 = wrapReporterAsV2;
  6. /**
  7. * Copyright (c) Microsoft Corporation.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. function wrapReporterAsV2(reporter) {
  22. try {
  23. if ('version' in reporter && reporter.version() === 'v2') return reporter;
  24. } catch (e) {}
  25. return new ReporterV2Wrapper(reporter);
  26. }
  27. class ReporterV2Wrapper {
  28. constructor(reporter) {
  29. this._reporter = void 0;
  30. this._deferred = [];
  31. this._config = void 0;
  32. this._reporter = reporter;
  33. }
  34. version() {
  35. return 'v2';
  36. }
  37. onConfigure(config) {
  38. this._config = config;
  39. }
  40. onBegin(suite) {
  41. var _this$_reporter$onBeg, _this$_reporter;
  42. (_this$_reporter$onBeg = (_this$_reporter = this._reporter).onBegin) === null || _this$_reporter$onBeg === void 0 ? void 0 : _this$_reporter$onBeg.call(_this$_reporter, this._config, suite);
  43. const deferred = this._deferred;
  44. this._deferred = null;
  45. for (const item of deferred) {
  46. if (item.error) this.onError(item.error);
  47. if (item.stdout) this.onStdOut(item.stdout.chunk, item.stdout.test, item.stdout.result);
  48. if (item.stderr) this.onStdErr(item.stderr.chunk, item.stderr.test, item.stderr.result);
  49. }
  50. }
  51. onTestBegin(test, result) {
  52. var _this$_reporter$onTes, _this$_reporter2;
  53. (_this$_reporter$onTes = (_this$_reporter2 = this._reporter).onTestBegin) === null || _this$_reporter$onTes === void 0 ? void 0 : _this$_reporter$onTes.call(_this$_reporter2, test, result);
  54. }
  55. onStdOut(chunk, test, result) {
  56. var _this$_reporter$onStd, _this$_reporter3;
  57. if (this._deferred) {
  58. this._deferred.push({
  59. stdout: {
  60. chunk,
  61. test,
  62. result
  63. }
  64. });
  65. return;
  66. }
  67. (_this$_reporter$onStd = (_this$_reporter3 = this._reporter).onStdOut) === null || _this$_reporter$onStd === void 0 ? void 0 : _this$_reporter$onStd.call(_this$_reporter3, chunk, test, result);
  68. }
  69. onStdErr(chunk, test, result) {
  70. var _this$_reporter$onStd2, _this$_reporter4;
  71. if (this._deferred) {
  72. this._deferred.push({
  73. stderr: {
  74. chunk,
  75. test,
  76. result
  77. }
  78. });
  79. return;
  80. }
  81. (_this$_reporter$onStd2 = (_this$_reporter4 = this._reporter).onStdErr) === null || _this$_reporter$onStd2 === void 0 ? void 0 : _this$_reporter$onStd2.call(_this$_reporter4, chunk, test, result);
  82. }
  83. onTestEnd(test, result) {
  84. var _this$_reporter$onTes2, _this$_reporter5;
  85. (_this$_reporter$onTes2 = (_this$_reporter5 = this._reporter).onTestEnd) === null || _this$_reporter$onTes2 === void 0 ? void 0 : _this$_reporter$onTes2.call(_this$_reporter5, test, result);
  86. }
  87. async onEnd(result) {
  88. var _this$_reporter$onEnd, _this$_reporter6;
  89. return await ((_this$_reporter$onEnd = (_this$_reporter6 = this._reporter).onEnd) === null || _this$_reporter$onEnd === void 0 ? void 0 : _this$_reporter$onEnd.call(_this$_reporter6, result));
  90. }
  91. async onExit() {
  92. var _this$_reporter$onExi, _this$_reporter7;
  93. await ((_this$_reporter$onExi = (_this$_reporter7 = this._reporter).onExit) === null || _this$_reporter$onExi === void 0 ? void 0 : _this$_reporter$onExi.call(_this$_reporter7));
  94. }
  95. onError(error) {
  96. var _this$_reporter$onErr, _this$_reporter8;
  97. if (this._deferred) {
  98. this._deferred.push({
  99. error
  100. });
  101. return;
  102. }
  103. (_this$_reporter$onErr = (_this$_reporter8 = this._reporter).onError) === null || _this$_reporter$onErr === void 0 ? void 0 : _this$_reporter$onErr.call(_this$_reporter8, error);
  104. }
  105. onStepBegin(test, result, step) {
  106. var _this$_reporter$onSte, _this$_reporter9;
  107. (_this$_reporter$onSte = (_this$_reporter9 = this._reporter).onStepBegin) === null || _this$_reporter$onSte === void 0 ? void 0 : _this$_reporter$onSte.call(_this$_reporter9, test, result, step);
  108. }
  109. onStepEnd(test, result, step) {
  110. var _this$_reporter$onSte2, _this$_reporter10;
  111. (_this$_reporter$onSte2 = (_this$_reporter10 = this._reporter).onStepEnd) === null || _this$_reporter$onSte2 === void 0 ? void 0 : _this$_reporter$onSte2.call(_this$_reporter10, test, result, step);
  112. }
  113. printsToStdio() {
  114. return this._reporter.printsToStdio ? this._reporter.printsToStdio() : true;
  115. }
  116. }