Repository: cypress-io/cypress
File: packages/server/lib/socket.js (JavaScript)

Code before:
    this.ended = false

    this.onTestFileChange = this.onTestFileChange.bind(this)
    this.onStudioTestFileChange = this.onStudioTestFileChange.bind(this)
    this.removeOnStudioTestFileChange = this.removeOnStudioTestFileChange.bind(this)

    if (config.watchForFileChanges) {
      preprocessor.emitter.on('file:updated', this.onTestFileChange)
    }
  }

  onTestFileChange (filePath) {
    debug('test file changed %o', filePath)

    return fs.statAsync(filePath)
    .then(() => {
      return this.io.emit('watched:file:changed')
    }).catch(() => {
      return debug('could not find test file that changed %o', filePath)
    })
  }

  onStudioTestFileChange (filePath) {
    return this.onTestFileChange(filePath).then(() => {
      this.removeOnStudioTestFileChange()
    })
  }

  removeOnStudioTestFileChange () {
    return preprocessor.emitter.off('file:updated', this.onStudioTestFileChange)
  }

  watchTestFileByPath (config, specConfig) {
    debug('watching spec with config %o', specConfig)

    const cleanIntegrationPrefix = (s) => {
      const removedIntegrationPrefix = path.join(config.integrationFolder, s.replace(`integration${path.sep}`, ''))

      return path.relative(config.projectRoot, removedIntegrationPrefix)
    }

    // previously we have assumed that we pass integration spec path with "integration/" prefix
    // now we pass spec config object that tells what kind of spec it is, has relative path already
    // so the only special handling remains for special paths like "integration/__all"
    const filePath = typeof specConfig === 'string' ? cleanIntegrationPrefix(specConfig) : specConfig.relative

    // bail if this is special path like "__all"
    // maybe the client should not ask to watch non-spec files?
    if (isSpecialSpec(filePath)) {
      return
    }

Review comment (suggestion):
may be worth adding a comment? was non-obvious to me what this was doing
```suggestion
    // wait for the studio test file to be written to disk, then reload the test
    return this.onTestFileChange(filePath).then(() => {
```

Code after:
    this.ended = false

    this.onTestFileChange = this.onTestFileChange.bind(this)
    this.onStudioTestFileChange = this.onStudioTestFileChange.bind(this)
    this.removeOnStudioTestFileChange = this.removeOnStudioTestFileChange.bind(this)

    if (config.watchForFileChanges) {
      preprocessor.emitter.on('file:updated', this.onTestFileChange)
    }
  }

  onTestFileChange (filePath) {
    debug('test file changed %o', filePath)

    return fs.statAsync(filePath)
    .then(() => {
      return this.io.emit('watched:file:changed')
    }).catch(() => {
      return debug('could not find test file that changed %o', filePath)
    })
  }

  onStudioTestFileChange (filePath) {
    // wait for the studio test file to be written to disk, then reload the test
    // and remove the listener (since this handler is only invoked when watchForFileChanges is false)
    return this.onTestFileChange(filePath).then(() => {
      this.removeOnStudioTestFileChange()
    })
  }

  removeOnStudioTestFileChange () {
    return preprocessor.emitter.off('file:updated', this.onStudioTestFileChange)
  }

  watchTestFileByPath (config, specConfig) {
    debug('watching spec with config %o', specConfig)

    const cleanIntegrationPrefix = (s) => {
      const removedIntegrationPrefix = path.join(config.integrationFolder, s.replace(`integration${path.sep}`, ''))

      return path.relative(config.projectRoot, removedIntegrationPrefix)
    }

    // previously we have assumed that we pass integration spec path with "integration/" prefix
    // now we pass spec config object that tells what kind of spec it is, has relative path already
    // so the only special handling remains for special paths like "integration/__all"
    const filePath = typeof specConfig === 'string' ? cleanIntegrationPrefix(specConfig) : specConfig.relative

    // bail if this is special path like "__all"
    // maybe the client should not ask to watch non-spec files?
    if (isSpecialSpec(filePath)) {