'How to test if the type of the result is a javascript 'function' in Jest?

How to properly test (using jest) whether the result is an actual JavaScript function?

describe('', () => {
    it('test', () => {
        const theResult = somethingThatReturnsAFunction();
        // how to check if theResult is a function
    });
});

The only solution I found is by using typeof like this:

    expect(typeof handledException === 'function').toEqual(true);

Is this the correct approach?



Solution 1:[1]

Jest offers a nice way to check the type of a supplied value.

You can use .toEqual(expect.any(<Constructor>)) to check if the provided value is of the constructor's type:

describe('', () => {
  it('test', () => {
    const theResult = somethingThatReturnsAFunction()
    expect(theResult).toEqual(expect.any(Function))
  })
})

Other examples of constructors are: String & Number.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marnix.hoh