How to mock get() of Histogram metrics

29 views Asked by At
export class Histogram<T extends string = string> {
    /**
     * @param configuration Configuration when creating the Histogram. Name and Help is mandatory
     */
    constructor(configuration: HistogramConfiguration<T>);

    /**
     * Observe value
     * @param value The value to observe
     */
    observe(value: number): void;
    /**
     * Observe value for given labels
     * @param labels Object with label keys and values
     * @param value The value to observe
     */
    observe(labels: LabelValues<T>, value: number): void;

    /**
     * Observe with exemplars
     * @param observeData Object with labels, value and exemplars for an observation
     */
    observe(observeData: ObserveDataWithExemplar<T>): void;

    /**
     * Get histogram metric object
     */
    get(): Promise<MetricObjectWithValues<MetricValueWithName<T>>>;

    /**
     * Start a timer. Calling the returned function will observe the duration in
     * seconds in the histogram.
     * @param labels Object with label keys and values
     * @return Function to invoke when timer should be stopped. The value it
     * returns is the timed duration.
     */
    startTimer(labels?: LabelValues<T>): (labels?: LabelValues<T>) => number;

    /**
     * Reset histogram values
     */
    reset(): void;

    /**
     * Initialize the metrics for the given combination of labels to zero
     */
    zero(labels: LabelValues<T>): void;

    /**
     * Return the child for given labels
     * @param values Label values
     * @return Configured histogram with given labels
     */
    labels(...values: string[]): Histogram.Internal<T>;

    /**
     * Return the child for given labels
     * @param labels Object with label keys and values
     * @return Configured counter with given labels
     */
    labels(labels: LabelValues<T>): Histogram.Internal<T>;

    /**
     * Remove metrics for the given label values
     * @param values Label values
     */
    remove(...values: string[]): void;

    /**
     * Remove metrics for the given label values
     * @param labels Object with label keys and values
     */
    remove(labels: LabelValues<T>): void;
}
}

This is the Histogram class of prom-client I want to write a mockHistogram class like this

export class MockHistogram extends MockMetric {
  constructor(metricName: string) {
    super(metricName)
  }

  mockTimer = jest.fn()

  mockLabels = jest.fn<
    ReturnType<MonitorUrlHistogram['labels']>,
    Parameters<MonitorUrlHistogram['labels']>
  >()

  labels = (...args: Parameters<MonitorUrlHistogram['labels']>) => {
    this.mockLabels(...args)
    return this
  }

  mockObserve = jest.fn<
    ReturnType<MonitorUrlHistogram['observe']>,
    Parameters<any>
  >()

  observe = (...args: Parameters<any>) => {
    this.mockObserve(...args)
    return this
  }

  mockZero = jest.fn<ReturnType<MonitorUrlHistogram['zero']>, Parameters<any>>()

  zero = (...args: Parameters<any>) => {
    this.mockZero(...args)
    return this
  }

  mockStartTimer = jest.fn<
    ReturnType<MonitorUrlHistogram['startTimer']>,
    Parameters<MonitorUrlHistogram['startTimer']>
  >()

  startTimer = (...args: Parameters<MonitorUrlHistogram['startTimer']>) => {
    this.mockStartTimer(...args)
    return this.mockTimer
  }

  mockReset = jest.fn<
    ReturnType<MonitorUrlHistogram['reset']>,
    Parameters<MonitorUrlHistogram['reset']>
  >()

  reset = (...args: Parameters<MonitorUrlHistogram['reset']>) => {
    this.mockReset(...args)
    return this
  }

  mockRemove = jest.fn<
    ReturnType<MonitorUrlHistogram['remove']>,
    Parameters<MonitorUrlHistogram['remove']>
  >()

  remove = (...args: Parameters<MonitorUrlHistogram['remove']>) => {
    this.mockRemove(...args)
    return this
  }
}

but when I try for get(), it's giving

The types returned by 'get()' are incompatible between these types. Type 'MockHistogram' is missing the following properties from type 'Promise<MetricObjectWithValues<MetricValueWithName<"host" | "method" | "path" | "status">>>'

0

There are 0 answers