code: function(opts) {
          opts = opts || {};
  
          if (this.expectedRequests.length === 0) {
              // calling flushAllExpected when there are no expectations is a
              // silly thing to do, and probably means that your test isn't
              // doing what you think it is doing (or it is racy). Hence we
              // reject this, rather than resolving immediately.
              return Promise.reject(new Error(
                  `flushAllExpected called with an empty expectation list`,
              ));
          }
  
          const waitTime = opts.timeout === undefined ? 1000 : opts.timeout;
          const endTime = waitTime + Date.now();
          let flushed = 0;
  
          const iterate = () => {
              const timeRemaining = endTime - Date.now();
              if (timeRemaining <= 0) {
                  throw new Error(
                      `Timed out after flushing ${flushed} requests; `+
                      `${this.expectedRequests.length} remaining`,
                  );
              }
  
              return this.flush(
                  undefined, undefined, timeRemaining,
              ).then((f) => {
                  flushed += f;
  
                  if (this.expectedRequests.length === 0) {
                      // we're done
                      return null;
                  }
  
                  return iterate();
              });
          };
  
          const prom = new Promise((resolve, reject) => {
              iterate().then(() => {
                  resolve(flushed);
              }, (e) => {
                  reject(e);
              });
          });
          this._flushPromises.push(prom);
          return prom;
      }
docs: Repeatedly flush requests until the list of expectations is empty.
  
  There is a total timeout (of 1000ms by default), after which the returned
  promise is rejected.
  
  @param {Object=} opts Options object
  @param {Number=} opts.timeout Total timeout, in ms. 1000 by default.
  @return {Promise} resolves when there is nothing left to flush, with the
  number of requests flushed