Repository: cypress-io/cypress
File: packages/app/src/runner/event-manager.ts (TypeScript)

Code before:
      window.top.removeEventListener('message', crossOriginOnMessageRef, false)
      crossOriginOnMessageRef = ({ data, source }) => {
        Cypress?.primaryOriginCommunicator.onMessage({ data, source })

        return undefined
      }

      window.top.addEventListener('message', crossOriginOnMessageRef, false)
    } catch (error) {
      // in cy-in-cy tests, window.top may not be accessible due to cross-origin restrictions
      if (error.name !== 'SecurityError') {
        // re-throw any error that's not a cross-origin error
        throw error
      }
    }
  }

  _runDriver (runState: RunState, testState: CachedTestState) {
    performance.mark('run-s')
    Cypress.run(testState, () => {
      performance.mark('run-e')
      performance.measure('run', 'run-s', 'run-e')
    })

    const hasActiveStudio = !!this.studioStore.testId ||
                           (!!this.studioStore.suiteId && this.studioStore._hasStarted) ||
                           !!this.studioStore.newTestLineNumber

    const studioSingleTestActive = this.studioStore.newTestLineNumber != null || !!this.studioStore.testId

    this.reporterBus.emit('reporter:start', {
      startTime: Cypress.runner.getStartTime(),
      numPassed: runState.passed,
      numFailed: runState.failed,
      numPending: runState.pending,
      autoScrollingEnabled: runState.autoScrollingEnabled,
      isSpecsListOpen: runState.isSpecsListOpen,
      scrollTop: runState.scrollTop,
      studioActive: hasActiveStudio,
      studioSingleTestActive,
    } as ReporterStartInfo)
  }

  stop () {
    this.localBus.removeAllListeners()

    // Grab existing listeners for url change event, we want to preserve them
    const urlChangeListeners = this.ws.listeners('change:to:url')

    this.ws.off()
    urlChangeListeners.forEach((listener) => this.ws.on('change:to:url', listener))

Review comment (suggestion):
I'm not sure this check is needed.
```ts
                           (!!this.studioStore.suiteId && this.studioStore._hasStarted) ||
```

Code after:
      window.top.removeEventListener('message', crossOriginOnMessageRef, false)
      crossOriginOnMessageRef = ({ data, source }) => {
        Cypress?.primaryOriginCommunicator.onMessage({ data, source })

        return undefined
      }

      window.top.addEventListener('message', crossOriginOnMessageRef, false)
    } catch (error) {
      // in cy-in-cy tests, window.top may not be accessible due to cross-origin restrictions
      if (error.name !== 'SecurityError') {
        // re-throw any error that's not a cross-origin error
        throw error
      }
    }
  }

  _runDriver (runState: RunState, testState: CachedTestState) {
    performance.mark('run-s')
    Cypress.run(testState, () => {
      performance.mark('run-e')
      performance.measure('run', 'run-s', 'run-e')
    })

    const hasActiveStudio = !!this.studioStore.testId ||
                           !!this.studioStore.newTestLineNumber

    const studioSingleTestActive = this.studioStore.newTestLineNumber != null || !!this.studioStore.testId

    this.reporterBus.emit('reporter:start', {
      startTime: Cypress.runner.getStartTime(),
      numPassed: runState.passed,
      numFailed: runState.failed,
      numPending: runState.pending,
      autoScrollingEnabled: runState.autoScrollingEnabled,
      isSpecsListOpen: runState.isSpecsListOpen,
      scrollTop: runState.scrollTop,
      studioActive: hasActiveStudio,
      studioSingleTestActive,
    } as ReporterStartInfo)
  }

  stop () {
    this.localBus.removeAllListeners()

    // Grab existing listeners for url change event, we want to preserve them
    const urlChangeListeners = this.ws.listeners('change:to:url')

    this.ws.off()
    urlChangeListeners.forEach((listener) => this.ws.on('change:to:url', listener))
  }