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

Code before:
  }

  private _finished(result: { newJob?: TestGroup, didFail: boolean }) {
    eventsHelper.removeEventListeners(this._listeners);
    this.jobResult.resolve(result);
  }

  runInWorker(worker: WorkerHost) {
    this._parallelIndex = worker.parallelIndex;
    this._workerIndex = worker.workerIndex;

    const runPayload: RunPayload = {
      file: this.job.requireFile,
      entries: this.job.tests.map(test => {
        return { testId: test.id, retry: test.results.length };
      }),
    };
    worker.runTestGroup(runPayload);

    this._listeners = [
      eventsHelper.addEventListener(worker, 'testBegin', this._onTestBegin.bind(this)),
      eventsHelper.addEventListener(worker, 'testEnd', this._onTestEnd.bind(this)),
      eventsHelper.addEventListener(worker, 'stepBegin', this._onStepBegin.bind(this)),
      eventsHelper.addEventListener(worker, 'stepEnd', this._onStepEnd.bind(this)),
      eventsHelper.addEventListener(worker, 'attach', this._onAttach.bind(this)),
      eventsHelper.addEventListener(worker, 'errors', this._onErrors.bind(this)),
      eventsHelper.addEventListener(worker, 'testPaused', this._onTestPaused.bind(this, worker)),
      eventsHelper.addEventListener(worker, 'done', this._onDone.bind(this)),
      eventsHelper.addEventListener(worker, 'exit', this.onExit.bind(this)),
    ];
  }

  private _onTestPaused(worker: WorkerHost, params: TestPausedPayload) {
    const sendMessage = async (message: { request: any }) => {
      try {
        if (this.jobResult.isDone())
          throw new Error('Test has already stopped');
        const response = await worker.sendCustomMessage({ testId: params.testId, request: message.request });
        if (response.error)
          addLocationAndSnippetToError(this._config.config, response.error);
        return response;
      } catch (e) {
        const error = serializeError(e);
        addLocationAndSnippetToError(this._config.config, error);
        return { response: undefined, error };
      }
    };

    const data = this._dataByTestId.get(params.testId);
    if (!data)
      return;

Review comment (suggestion):
Let's call it `testErrors` to avoid confusion with "worker errors".

Code after:
    const newJob = remaining.length ? { ...this.job, tests: remaining } : undefined;
    this._finished({ didFail: true, newJob });
  }

  onExit(data: ProcessExitData) {
    const unexpectedExitError: TestError | undefined = data.unexpectedly ? {
      message: `Error: worker process exited unexpectedly (code=${data.code}, signal=${data.signal})`
    } : undefined;
    this._onDone({ skipTestsDueToSetupFailure: [], fatalErrors: [], unexpectedExitError });
  }

  private _finished(result: { newJob?: TestGroup, didFail: boolean }) {
    eventsHelper.removeEventListeners(this._listeners);
    this.jobResult.resolve(result);
  }

  runInWorker(worker: WorkerHost) {
    this._parallelIndex = worker.parallelIndex;
    this._workerIndex = worker.workerIndex;

    const runPayload: RunPayload = {
      file: this.job.requireFile,
      entries: this.job.tests.map(test => {
        return { testId: test.id, retry: test.results.length };
      }),
    };
    worker.runTestGroup(runPayload);

    this._listeners = [
      eventsHelper.addEventListener(worker, 'testBegin', this._onTestBegin.bind(this)),
      eventsHelper.addEventListener(worker, 'testEnd', this._onTestEnd.bind(this)),
      eventsHelper.addEventListener(worker, 'stepBegin', this._onStepBegin.bind(this)),
      eventsHelper.addEventListener(worker, 'stepEnd', this._onStepEnd.bind(this)),
      eventsHelper.addEventListener(worker, 'attach', this._onAttach.bind(this)),
      eventsHelper.addEventListener(worker, 'testErrors', this._onTestErrors.bind(this)),
      eventsHelper.addEventListener(worker, 'testPaused', this._onTestPaused.bind(this, worker)),
      eventsHelper.addEventListener(worker, 'done', this._onDone.bind(this)),
      eventsHelper.addEventListener(worker, 'exit', this.onExit.bind(this)),
    ];
  }

  private _onTestPaused(worker: WorkerHost, params: TestPausedPayload) {
    const data = this._dataByTestId.get(params.testId);
    if (!data)
      return;

    const { test, result } = data;

    const sendMessage = async (message: { request: any }) => {
      try {
        if (this.jobResult.isDone())