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

Code before:
  onConfigure(config: FullConfig) {
    this._config = config;
    this._startTime = new Date();
    this._monotonicStartTime = monotonicTime();
    this._reporter.onConfigure?.(config);
  }

  onBegin(suite: Suite) {
    this._didBegin = true;
    this._reporter.onBegin?.(suite);
  }

  onTestBegin(test: TestCase, result: TestResult) {
    this._reporter.onTestBegin?.(test, result);
  }

  onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    this._reporter.onStdOut?.(chunk, test, result);
  }

  onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    this._reporter.onStdErr?.(chunk, test, result);
  }

  async onTestPaused(test: TestCase, result: TestResult, step: TestStep) {
    this._addSnippetToTestErrors(test, result);
    this._addSnippetToStepError(test, step);
    return await this._reporter.onTestPaused?.(test, result, step);
  }

  onTestEnd(test: TestCase, result: TestResult) {
    this._addSnippetToTestErrors(test, result);
    this._reporter.onTestEnd?.(test, result);
  }

  async onEnd(result: { status: FullResult['status'] }) {
    if (!this._didBegin) {
      // onBegin was not reported, emit it.
      this.onBegin(new Suite('', 'root'));
    }
    return await this._reporter.onEnd?.({
      ...result,
      startTime: this._startTime!,
      duration: monotonicTime() - this._monotonicStartTime!,
    });
  }

  async onExit() {
    await this._reporter.onExit?.();
  }


Review comment (suggestion):
Let's early return when `!!error.snippet` from `addLocationAndSnippetToError` to mitigate double snippet calculation.

Code after:
  onConfigure(config: FullConfig) {
    this._config = config;
    this._startTime = new Date();
    this._monotonicStartTime = monotonicTime();
    this._reporter.onConfigure?.(config);
  }

  onBegin(suite: Suite) {
    this._didBegin = true;
    this._reporter.onBegin?.(suite);
  }

  onTestBegin(test: TestCase, result: TestResult) {
    this._reporter.onTestBegin?.(test, result);
  }

  onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    this._reporter.onStdOut?.(chunk, test, result);
  }

  onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    this._reporter.onStdErr?.(chunk, test, result);
  }

  async onTestPaused(test: TestCase, result: TestResult, step?: TestStep) {
    this._addSnippetToTestErrors(test, result);
    if (step)
      this._addSnippetToStepError(test, step);
    return await this._reporter.onTestPaused?.(test, result, step) ?? { action: undefined };
  }

  onTestEnd(test: TestCase, result: TestResult) {
    this._addSnippetToTestErrors(test, result);
    this._reporter.onTestEnd?.(test, result);
  }

  async onEnd(result: { status: FullResult['status'] }) {
    if (!this._didBegin) {
      // onBegin was not reported, emit it.
      this.onBegin(new Suite('', 'root'));
    }
    return await this._reporter.onEnd?.({
      ...result,
      startTime: this._startTime!,
      duration: monotonicTime() - this._monotonicStartTime!,
    });
  }

  async onExit() {
    await this._reporter.onExit?.();
  }