line.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _base = require("./base");
  7. /**
  8. * Copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. class LineReporter extends _base.BaseReporter {
  23. constructor(...args) {
  24. super(...args);
  25. this._current = 0;
  26. this._failures = 0;
  27. this._lastTest = void 0;
  28. this._didBegin = false;
  29. }
  30. printsToStdio() {
  31. return true;
  32. }
  33. onBegin(suite) {
  34. super.onBegin(suite);
  35. const startingMessage = this.generateStartingMessage();
  36. if (startingMessage) {
  37. console.log(startingMessage);
  38. console.log();
  39. }
  40. this._didBegin = true;
  41. }
  42. onStdOut(chunk, test, result) {
  43. super.onStdOut(chunk, test, result);
  44. this._dumpToStdio(test, chunk, process.stdout);
  45. }
  46. onStdErr(chunk, test, result) {
  47. super.onStdErr(chunk, test, result);
  48. this._dumpToStdio(test, chunk, process.stderr);
  49. }
  50. _dumpToStdio(test, chunk, stream) {
  51. if (this.config.quiet) return;
  52. if (!process.env.PW_TEST_DEBUG_REPORTERS) stream.write(`\u001B[1A\u001B[2K`);
  53. if (test && this._lastTest !== test) {
  54. // Write new header for the output.
  55. const title = _base.colors.dim((0, _base.formatTestTitle)(this.config, test));
  56. stream.write(this.fitToScreen(title) + `\n`);
  57. this._lastTest = test;
  58. }
  59. stream.write(chunk);
  60. if (chunk[chunk.length - 1] !== '\n') console.log();
  61. console.log();
  62. }
  63. onTestBegin(test, result) {
  64. super.onTestBegin(test, result);
  65. ++this._current;
  66. this._updateLine(test, result, undefined);
  67. }
  68. onStepBegin(test, result, step) {
  69. super.onStepBegin(test, result, step);
  70. if (step.category === 'test.step') this._updateLine(test, result, step);
  71. }
  72. onStepEnd(test, result, step) {
  73. super.onStepEnd(test, result, step);
  74. if (step.category === 'test.step') this._updateLine(test, result, step.parent);
  75. }
  76. onTestEnd(test, result) {
  77. super.onTestEnd(test, result);
  78. if (!this.willRetry(test) && (test.outcome() === 'flaky' || test.outcome() === 'unexpected' || result.status === 'interrupted')) {
  79. if (!process.env.PW_TEST_DEBUG_REPORTERS) process.stdout.write(`\u001B[1A\u001B[2K`);
  80. console.log((0, _base.formatFailure)(this.config, test, {
  81. index: ++this._failures
  82. }).message);
  83. console.log();
  84. }
  85. }
  86. _updateLine(test, result, step) {
  87. const retriesPrefix = this.totalTestCount < this._current ? ` (retries)` : ``;
  88. const prefix = `[${this._current}/${this.totalTestCount}]${retriesPrefix} `;
  89. const currentRetrySuffix = result.retry ? _base.colors.yellow(` (retry #${result.retry})`) : '';
  90. const title = (0, _base.formatTestTitle)(this.config, test, step) + currentRetrySuffix;
  91. if (process.env.PW_TEST_DEBUG_REPORTERS) process.stdout.write(`${prefix + title}\n`);else process.stdout.write(`\u001B[1A\u001B[2K${prefix + this.fitToScreen(title, prefix)}\n`);
  92. }
  93. onError(error) {
  94. super.onError(error);
  95. const message = (0, _base.formatError)(error, _base.colors.enabled).message + '\n';
  96. if (!process.env.PW_TEST_DEBUG_REPORTERS && this._didBegin) process.stdout.write(`\u001B[1A\u001B[2K`);
  97. process.stdout.write(message);
  98. console.log();
  99. }
  100. async onEnd(result) {
  101. if (!process.env.PW_TEST_DEBUG_REPORTERS && this._didBegin) process.stdout.write(`\u001B[1A\u001B[2K`);
  102. await super.onEnd(result);
  103. this.epilogue(false);
  104. }
  105. }
  106. var _default = exports.default = LineReporter;