Repository: cypress-io/cypress
File: packages/server/test/unit/browsers/browsers_spec.ts (TypeScript)

Code before:
    it('sets didBrowserPreviouslyHaveUnexpectedExit when the browser unexpectedly closes', () => {
      const url: TestUrl = 'http://localhost:3000'
      const browserInstance = new EventEmitter() as BrowserInstance

      browserInstance.kill = () => {
        browserInstance.emit('exit')
      }

      const instance = browserInstance

      browsers._setInstance(instance)

      sinon.stub(electron, 'open').resolves(instance)
      sinon.spy(ctx.actions.app, 'setBrowserStatus')

      // Stub to speed up test, we don't care about the delay
      sinon.stub(Promise, 'delay').resolves()

      return browsers.open({ name: 'electron', family: 'chromium' } as any, { url } as any, null, ctx).then(browsers.close).then(() => {
        expect(ctx.coreData.didBrowserPreviouslyHaveUnexpectedExit).eq(true)
      })
    })
  })

  context('browser cleanup', () => {
    it('calls onBrowserClose callback on close', async () => {
      const onBrowserClose = sinon.stub()
      const url: TestUrl = 'http://localhost:3000'
      const browserInstance = new EventEmitter() as BrowserInstance

      browserInstance.kill = () => {
        browserInstance.emit('exit')
      }

      const instance = browserInstance

      browsers._setInstance(instance)

      sinon.stub(electron, 'open').resolves(instance)
      sinon.spy(ctx.actions.app, 'setBrowserStatus')

      // Stub to speed up test, we don't care about the delay
      sinon.stub(Promise, 'delay').resolves()

      return browsers.open({ name: 'electron', family: 'chromium' } as any, { url, onBrowserClose } as any, null, ctx).then(() => {
        // Simulate browser exit
        browserInstance.emit('exit')

        expect(onBrowserClose).to.be.called
      })
    })

Review comment (suggestion):
these shouldn't need to be async in their current state unless you want to rework the function to await `browsers.open`
```suggestion
    it('calls onBrowserClose callback on close', () => {
```

Code after:
    it('sets didBrowserPreviouslyHaveUnexpectedExit when the browser unexpectedly closes', () => {
      const url: TestUrl = 'http://localhost:3000'
      const browserInstance = new EventEmitter() as BrowserInstance

      browserInstance.kill = () => {
        browserInstance.emit('exit')
      }

      const instance = browserInstance

      browsers._setInstance(instance)

      sinon.stub(electron, 'open').resolves(instance)
      sinon.spy(ctx.actions.app, 'setBrowserStatus')

      // Stub to speed up test, we don't care about the delay
      sinon.stub(Promise, 'delay').resolves()

      return browsers.open({ name: 'electron', family: 'chromium' } as any, { url } as any, null, ctx).then(browsers.close).then(() => {
        expect(ctx.coreData.didBrowserPreviouslyHaveUnexpectedExit).eq(true)
      })
    })
  })

  context('browser cleanup', () => {
    it('calls onBrowserClose callback on close', () => {
      const onBrowserClose = sinon.stub()
      const url: TestUrl = 'http://localhost:3000'
      const browserInstance = new EventEmitter() as BrowserInstance

      browserInstance.kill = () => {
        browserInstance.emit('exit')
      }

      const instance = browserInstance

      browsers._setInstance(instance)

      sinon.stub(electron, 'open').resolves(instance)
      sinon.spy(ctx.actions.app, 'setBrowserStatus')

      // Stub to speed up test, we don't care about the delay
      sinon.stub(Promise, 'delay').resolves()

      return browsers.open({ name: 'electron', family: 'chromium' } as any, { url, onBrowserClose } as any, null, ctx).then(() => {
        // Simulate browser exit
        browserInstance.emit('exit')

        expect(onBrowserClose).to.be.called
      })
    })