Repository: cypress-io/cypress
File: packages/driver/src/cypress/cy.js (JavaScript)

Code before:
      }

      contentWindow.HTMLInputElement.prototype.select = function () {
        return $selection.interceptSelect.call(this)
      }

      contentWindow.document.hasFocus = function () {
        return focused.documentHasFocus.call(this)
      }

      const cssModificationSpy = function (original, ...args) {
        snapshots.onCssModified(this.href)

        return original.apply(this, args)
      }

      const { insertRule } = contentWindow.CSSStyleSheet.prototype
      const { deleteRule } = contentWindow.CSSStyleSheet.prototype

      contentWindow.CSSStyleSheet.prototype.insertRule = _.wrap(insertRule, cssModificationSpy)
      contentWindow.CSSStyleSheet.prototype.deleteRule = _.wrap(deleteRule, cssModificationSpy)

      // drop "fetch" polyfill that replaces it with XMLHttpRequest
      // by using "eval" we force the polyfill to use XMLHttpRequest objects
      // from the app iframe that we wrap for network stubbing
      contentWindow.eval(`fetch=${fetchPolyfill}`)
    } catch (error) {} // eslint-disable-line no-empty
  }

  const enqueue = function (obj) {
    // if we have a nestedIndex it means we're processing
    // nested commands and need to splice them into the
    // index past the current index as opposed to
    // pushing them to the end we also dont want to
    // reset the run defer because splicing means we're
    // already in a run loop and dont want to create another!
    // we also reset the .next property to properly reference
    // our new obj

    // we had a bug that would bomb on custom commands when it was the
    // first command. this was due to nestedIndex being undefined at that
    // time. so we have to ensure to check that its any kind of number (even 0)
    // in order to know to splice into the existing array.
    let nestedIndex = state('nestedIndex')

    // if this is a number then we know
    // we're about to splice this into our commands
    // and need to reset next + increment the index
    if (_.isNumber(nestedIndex)) {
      state('nestedIndex', (nestedIndex += 1))
    }

Review comment (suggestion):
I think we can pretty easily avoid eval + toString and use the correct XMLHttpRequest object if we patch-package and take in the XMLHttpRequest (or content window) reference

Code after:

      contentWindow.HTMLInputElement.prototype.select = function () {
        return $selection.interceptSelect.call(this)
      }

      contentWindow.document.hasFocus = function () {
        return focused.documentHasFocus.call(this)
      }

      const cssModificationSpy = function (original, ...args) {
        snapshots.onCssModified(this.href)

        return original.apply(this, args)
      }

      const { insertRule } = contentWindow.CSSStyleSheet.prototype
      const { deleteRule } = contentWindow.CSSStyleSheet.prototype

      contentWindow.CSSStyleSheet.prototype.insertRule = _.wrap(insertRule, cssModificationSpy)
      contentWindow.CSSStyleSheet.prototype.deleteRule = _.wrap(deleteRule, cssModificationSpy)

      if (config('experimentalFetchPolyfill')) {
        // drop "fetch" polyfill that replaces it with XMLHttpRequest
        // from the app iframe that we wrap for network stubbing
        contentWindow.fetch = registerFetch(contentWindow)
        // flag the polyfill to test this experimental feature easier
        state('fetchPolyfilled', true)
      }
    } catch (error) {} // eslint-disable-line no-empty
  }

  const enqueue = function (obj) {
    // if we have a nestedIndex it means we're processing
    // nested commands and need to splice them into the
    // index past the current index as opposed to
    // pushing them to the end we also dont want to
    // reset the run defer because splicing means we're
    // already in a run loop and dont want to create another!
    // we also reset the .next property to properly reference
    // our new obj

    // we had a bug that would bomb on custom commands when it was the
    // first command. this was due to nestedIndex being undefined at that
    // time. so we have to ensure to check that its any kind of number (even 0)
    // in order to know to splice into the existing array.
    let nestedIndex = state('nestedIndex')

    // if this is a number then we know
    // we're about to splice this into our commands
    // and need to reset next + increment the index
    if (_.isNumber(nestedIndex)) {