function recordFunction(opt_f) {
  const f = opt_f || functions.UNDEFINED;
  const calls = [];
  /** @type {?Resolver} */
  let waitForCallsResolver = null;
  /** @type {number} */
  let waitForCallsCount = 0;

  function maybeResolveWaitForCalls() {
    if (waitForCallsResolver && calls.length >= waitForCallsCount) {
      waitForCallsResolver.resolve();
      waitForCallsResolver = null;
      waitForCallsCount = 0;
    }
  }

  /** @type {!recordFunction.Type} */
  function recordedFunction() {
    const owner = /** @type {?} */ (this);
    try {
      const ret = f.apply(owner, arguments);
      calls.push(new FunctionCall(f, owner, arguments, ret, null));
      maybeResolveWaitForCalls();
      return ret;
    } catch (err) {
      calls.push(new FunctionCall(f, owner, arguments, undefined, err));
      maybeResolveWaitForCalls();
      throw err;
    }
  }

  /**
   * @return {number} Total number of calls.
   */
  recordedFunction.getCallCount = function() {
    return calls.length;
  };

  /**
   * Asserts that the function was called a certain number of times.
   * @param {number|string} a The expected number of calls (1 arg) or debug
   *     message (2 args).
   * @param {number=} opt_b The expected number of calls (2 args only).
   */
  recordedFunction.assertCallCount = function(a, opt_b) {
    const actual = calls.length;
    const expected = arguments.length == 1 ? a : opt_b;
    const message = arguments.length == 1 ? '' : ' ' + a;
    assertEquals(
        'Expected ' + expected + ' call(s), but was ' + actual + '.' + message,
        expected, actual);
  };

  /**
   * @return {!Array<!FunctionCall>} All calls of the recorded
   *     function.
   */
  recordedFunction.getCalls = function() {
    return calls;
  };


  /**
   * @return {?FunctionCall} Last call of the recorded function or
   *     null if it hasn't been called.
   */
  recordedFunction.getLastCall = function() {
    return calls[calls.length - 1] || null;
  };

  /**
   * Returns and removes the last call of the recorded function.
   * @return {?FunctionCall} Last call of the recorded function or
   *     null if it hasn't been called.
   */
  recordedFunction.popLastCall = function() {
    return calls.pop() || null;
  };

  /**
   * Returns a GoogPromise that resolves when the recorded function has equal
   * to or greater than the number of calls.
   * @param {number} num
   * @return {!GoogPromise<undefined>}
   */
  recordedFunction.waitForCalls = function(num) {
    waitForCallsCount = num;
    waitForCallsResolver = GoogPromise.withResolver();
    const promise = waitForCallsResolver.promise;
    maybeResolveWaitForCalls();
    return promise;
  };

  /**
   * Resets the recorded function and removes all calls.
   */
  recordedFunction.reset = function() {
    calls.length = 0;
    waitForCallsResolver = null;
    waitForCallsCount = 0;
  };

  return recordedFunction;
}
Wraps the function into another one which calls the inner function and
records its calls. The recorded function will have 3 static methods:
`getCallCount`, `getCalls` and `getLastCall` but won't
inherit the original function's prototype and static fields.

@param {!Function=} opt_f The function to wrap and record. Defaults to
    functions.UNDEFINED.
@return {!recordFunction.Type} The wrapped function.