Skip to content

Returned functions by the Histogram labels method not testable #650

Description

@fermeaux

Issue

Hello, I have been trying to assert with jest that the call to the method observe behind a call to labels on an Histogram works.
However it doesn't because the function returned by the method labels are referencing directly the function without passing the reference to the corresponding method (observe and startTimer).
It is not a big deal because it is for testing purpose on my case but maybe someone need to keep the reference at some point.

Solution

Instead of returning

return {
    observe: observe.call(this, labels),
    startTimer: startTimer.call(this, labels),
};

We could return

return {
    observe: this.observe.bind(this, labels),
    startTimer: this.startTimer.bind(this, labels),
};

Alternative

It is not a blocking point at all.
On my side I changed the call I was making from histogram.labels(LABELS).observe(VALUE) to histogram.observe({ LABELS }, VALUE).

Concern

If we make change it means that the current behavior change too because it is not the same reference.
So maybe it is something wanted initially ?

Next steps

Can I make a pull request with that change ?
Or maybe someone has a work around with jest ?
I am spying the method like this
jest.spyOn(histogram, 'observe')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions