Repository: microsoft/playwright
File: packages/playwright/src/reporters/line.ts (TypeScript)

Code before:
    ++this._current;
    this._updateLine(test, result, undefined);
  }

  onStepBegin(test: TestCase, result: TestResult, step: TestStep) {
    if (this.screen.isTTY && step.category === 'test.step')
      this._updateLine(test, result, step);
  }

  onStepEnd(test: TestCase, result: TestResult, step: TestStep) {
    if (this.screen.isTTY && step.category === 'test.step')
      this._updateLine(test, result, step.parent);
  }

  async onTestPaused(test: TestCase, result: TestResult) {
    // without TTY, user cannot interrupt the pause. let's skip it.
    if (!process.stdin.isTTY && !process.env.PW_TEST_DEBUG_REPORTERS)
      return;

    if (!process.env.PW_TEST_DEBUG_REPORTERS)
      this.screen.stdout.write(`\u001B[1A\u001B[2K`);

    this._printedErrorCounts.set(result, result.errors.length);

    if (result.errors.length > 0) {
      const errorHeader = this.formatTestHeader(test, { indent: '  ', index: ++this._failures });
      this.screen.stdout.write(this.screen.colors.red(errorHeader) + '\n');
      const errorDetails = formatResultFailure(this.screen, test, result, '    ');
      for (const error of errorDetails)
        this.screen.stdout.write('\n' + error.message + '\n');
      this.screen.stdout.write(this.screen.colors.yellow('\n    Paused on error. Press Ctrl+C to end.') + '\n');
    } else {
      const header = this.formatTestHeader(test, { indent: '  ' });
      this.screen.stdout.write(this.screen.colors.yellow(header) + '\n');
      this.screen.stdout.write(this.screen.colors.yellow('    Paused at test end. Press Ctrl+C to end.') + '\n');
    }

    this.writeLine();
    this.writeLine();

    this._updateLine(test, result, undefined);

    await new Promise<void>(() => {});
  }

  override onTestEnd(test: TestCase, result: TestResult) {
    super.onTestEnd(test, result);

    const printedCount = this._printedErrorCounts.get(result);
    this._printedErrorCounts.delete(result);
    if (printedCount !== undefined) {

Review comment (refactor):
I think we can use a helper in `base.ts`, something like `formatSingleResult()`, that will include everything except for `Paused .... Press Ctrl+C to end` line.

Code after:
    this._updateLine(test, result, undefined);
  }

  onStepBegin(test: TestCase, result: TestResult, step: TestStep) {
    if (this.screen.isTTY && step.category === 'test.step')
      this._updateLine(test, result, step);
  }

  onStepEnd(test: TestCase, result: TestResult, step: TestStep) {
    if (this.screen.isTTY && step.category === 'test.step')
      this._updateLine(test, result, step.parent);
  }

  async onTestPaused(test: TestCase, result: TestResult) {
    // without TTY, user cannot interrupt the pause. let's skip it.
    if (!process.stdin.isTTY && !process.env.PW_TEST_DEBUG_REPORTERS)
      return;

    if (!process.env.PW_TEST_DEBUG_REPORTERS)
      this.screen.stdout.write(`\u001B[1A\u001B[2K`);

    this.writeLine(this.formatSingleResult(test, result, test.outcome() === 'unexpected' ? ++this._failures : undefined));
    markErrorsAsReported(result);
    this.writeLine(this.screen.colors.yellow(`    Paused ${test.outcome() === 'unexpected' ? 'on error' : 'at test end'}. Press Ctrl+C to end.`) + '\n\n');

    this._updateLine(test, result, undefined);

    await new Promise<void>(() => {});
  }

  override onTestEnd(test: TestCase, result: TestResult) {
    super.onTestEnd(test, result);
    if (!this.willRetry(test) && (test.outcome() === 'flaky' || test.outcome() === 'unexpected' || result.status === 'interrupted')) {
      if (!process.env.PW_TEST_DEBUG_REPORTERS)
        this.screen.stdout.write(`\u001B[1A\u001B[2K`);
      this.writeLine(this.formatFailure(test, ++this._failures));
      this.writeLine();
    }
  }

  private _updateLine(test: TestCase, result: TestResult, step?: TestStep) {
    const retriesPrefix = this.totalTestCount < this._current ? ` (retries)` : ``;
    const prefix = `[${this._current}/${this.totalTestCount}]${retriesPrefix} `;
    const currentRetrySuffix = result.retry ? this.screen.colors.yellow(` (retry #${result.retry})`) : '';
    const title = this.formatTestTitle(test, step) + currentRetrySuffix;
    if (process.env.PW_TEST_DEBUG_REPORTERS)
      this.screen.stdout.write(`${prefix + title}\n`);
    else
      this.screen.stdout.write(`\u001B[1A\u001B[2K${prefix + this.fitToScreen(title, prefix)}\n`);
  }