Repository: microsoft/playwright
File: packages/playwright/src/runner/dispatcher.ts (TypeScript)

Code before:
    }

    const data = this._dataByTestId.get(params.testId)!;
    if (!data)
      return;
    const { test, result } = data;
    result.errors.push(...params.errors);
    result.error = result.errors[0];
    for (const error of result.errors)
      this._reporter.onTestError?.(test, result, error);
  }

  private _failTestWithErrors(test: TestCase, errors: TestError[]) {
    const runData = this._dataByTestId.get(test.id);
    // There might be a single test that has started but has not finished yet.
    let result: TestResult;
    if (runData) {
      result = runData.result;
    } else {
      result = test._appendTestResult();
      this._reporter.onTestBegin?.(test, result);
    }
    result.errors.push(...errors);
    result.error = result.errors[0];
    for (const error of result.errors)
      this._reporter.onTestError?.(test, result, error);
    result.status = errors.length ? 'failed' : 'skipped';
    this._reportTestEnd(test, result);
    this._failedTests.add(test);
  }

  private _massSkipTestsFromRemaining(testIds: Set<string>, errors: TestError[]) {
    for (const test of this._remainingByTestId.values()) {
      if (!testIds.has(test.id))
        continue;
      if (!this._failureTracker.hasReachedMaxFailures()) {
        this._failTestWithErrors(test, errors);
        errors = []; // Only report errors for the first test.
      }
      this._remainingByTestId.delete(test.id);
    }
    if (errors.length) {
      // We had fatal errors after all tests have passed - most likely in some teardown.
      // Let's just fail the test run.
      this._failureTracker.onWorkerError();
      for (const error of errors)
        this._reporter.onError?.(error);
    }
  }

  private _onDone(params: DonePayload & { unexpectedExitError?: TestError }) {

Review comment (suggestion):
```suggestion
    for (const error of errors) {
      result.errors.push(error);
      result.error = result.errors[0];
      this._reporter.onTestError?.(test, result, error);
    }
```

Code after:
    }

    const data = this._dataByTestId.get(params.testId)!;
    if (!data)
      return;
    const { test, result } = data;
    for (const error of params.errors) {
      result.errors.push(error);
      result.error = result.errors[0];
      this._reporter.onTestError?.(test, result, error);
    }
  }

  private _failTestWithErrors(test: TestCase, errors: TestError[]) {
    const runData = this._dataByTestId.get(test.id);
    // There might be a single test that has started but has not finished yet.
    let result: TestResult;
    if (runData) {
      result = runData.result;
    } else {
      result = test._appendTestResult();
      this._reporter.onTestBegin?.(test, result);
    }
    for (const error of errors) {
      result.errors.push(error);
      result.error = result.errors[0];
      this._reporter.onTestError?.(test, result, error);
    }
    result.status = errors.length ? 'failed' : 'skipped';
    this._reportTestEnd(test, result);
    this._failedTests.add(test);
  }

  private _massSkipTestsFromRemaining(testIds: Set<string>, errors: TestError[]) {
    for (const test of this._remainingByTestId.values()) {
      if (!testIds.has(test.id))
        continue;
      if (!this._failureTracker.hasReachedMaxFailures()) {
        this._failTestWithErrors(test, errors);
        errors = []; // Only report errors for the first test.
      }
      this._remainingByTestId.delete(test.id);
    }
    if (errors.length) {
      // We had fatal errors after all tests have passed - most likely in some teardown.
      // Let's just fail the test run.
      this._failureTracker.onWorkerError();
      for (const error of errors)
        this._reporter.onError?.(error);
    }
  }