multiplexer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Multiplexer = void 0;
  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. class Multiplexer {
  22. constructor(reporters) {
  23. this._reporters = void 0;
  24. this._reporters = reporters;
  25. }
  26. version() {
  27. return 'v2';
  28. }
  29. onConfigure(config) {
  30. for (const reporter of this._reporters) wrap(() => reporter.onConfigure(config));
  31. }
  32. onBegin(suite) {
  33. for (const reporter of this._reporters) wrap(() => reporter.onBegin(suite));
  34. }
  35. onTestBegin(test, result) {
  36. for (const reporter of this._reporters) wrap(() => reporter.onTestBegin(test, result));
  37. }
  38. onStdOut(chunk, test, result) {
  39. for (const reporter of this._reporters) wrap(() => reporter.onStdOut(chunk, test, result));
  40. }
  41. onStdErr(chunk, test, result) {
  42. for (const reporter of this._reporters) wrap(() => reporter.onStdErr(chunk, test, result));
  43. }
  44. onTestEnd(test, result) {
  45. for (const reporter of this._reporters) wrap(() => reporter.onTestEnd(test, result));
  46. }
  47. async onEnd(result) {
  48. for (const reporter of this._reporters) {
  49. const outResult = await wrapAsync(() => reporter.onEnd(result));
  50. if (outResult !== null && outResult !== void 0 && outResult.status) result.status = outResult.status;
  51. }
  52. return result;
  53. }
  54. async onExit() {
  55. for (const reporter of this._reporters) await wrapAsync(() => reporter.onExit());
  56. }
  57. onError(error) {
  58. for (const reporter of this._reporters) wrap(() => reporter.onError(error));
  59. }
  60. onStepBegin(test, result, step) {
  61. for (const reporter of this._reporters) wrap(() => reporter.onStepBegin(test, result, step));
  62. }
  63. onStepEnd(test, result, step) {
  64. for (const reporter of this._reporters) wrap(() => reporter.onStepEnd(test, result, step));
  65. }
  66. printsToStdio() {
  67. return this._reporters.some(r => {
  68. let prints = true;
  69. wrap(() => prints = r.printsToStdio());
  70. return prints;
  71. });
  72. }
  73. }
  74. exports.Multiplexer = Multiplexer;
  75. async function wrapAsync(callback) {
  76. try {
  77. return await callback();
  78. } catch (e) {
  79. console.error('Error in reporter', e);
  80. }
  81. }
  82. function wrap(callback) {
  83. try {
  84. callback();
  85. } catch (e) {
  86. console.error('Error in reporter', e);
  87. }
  88. }