function stubXhrToReturn(status, opt_responseText, opt_latency) {
  if (opt_latency != null) {
    mockClock = new MockClock(true);
  }

  const stubXhr = {
    sent: false,
    aborted: false,
    status: 0,
    headers: {},
    open: function(method, url, async) {
      /** @suppress {globalThis} suppression added to enable type checking */
      this.method = method;
      /** @suppress {globalThis} suppression added to enable type checking */
      this.url = url;
      /** @suppress {globalThis} suppression added to enable type checking */
      this.async = async;
    },
    setRequestHeader: function(key, value) {
      /** @suppress {globalThis} suppression added to enable type checking */
      this.headers[key] = value;
    },
    overrideMimeType: function(mimeType) {
      /** @suppress {globalThis} suppression added to enable type checking */
      this.mimeType = mimeType;
    },
    abort: /**
              @suppress {globalThis} suppression added to enable type checking
            */
        function() {
          /**
           * @suppress {globalThis} suppression added to enable type
           * checking
           */
          this.aborted = true;
          this.load(0);
        },
    send: /**
             @suppress {globalThis} suppression added to enable type checking
           */
        function(data) {
          /**
           * @suppress {globalThis} suppression added to enable type
           * checking
           */
          this.data = data;
          /**
           * @suppress {globalThis} suppression added to enable type
           * checking
           */
          this.sent = true;

          // Fulfill the send asynchronously, or possibly with the
          // MockClock.
          window.setTimeout(
              goog.bind(this.load, this, status), opt_latency || 0);
          if (mockClock) {
            mockClock.tick(opt_latency);
          }
        },
    load: /**
             @suppress {globalThis} suppression added to enable type checking
           */
        function(status) {
          /**
           * @suppress {globalThis} suppression added to enable type
           * checking
           */
          this.status = status;
          if (opt_responseText != null) {
            /**
             * @suppress {globalThis} suppression added to enable type
             * checking
             */
            this.responseText = opt_responseText;
          }
          /**
           * @suppress {globalThis} suppression added to enable type
           * checking
           */
          this.readyState = 4;
          if (this.onreadystatechange) this.onreadystatechange();
        }
  };

  stubXmlHttpWith(stubXhr);
  return stubXhr;
}
@param {number} status HTTP status code.
@param {string=} opt_responseText
@param {number=} opt_latency
    Milliseconds between sending a request and receiving the response.
@return {!XhrLike} The XHR stub that will be returned from the factory.
@suppress {checkTypes} suppression added to enable type checking