Search code examples
javascriptunit-testingjestjs

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

  • You can use toBe matcher to check whether result of typeof operator is function, please see example:

    describe("", () => {
      it("test", () => {
        const somethingThatReturnsAFunction = () => () => {};
        const theResult = somethingThatReturnsAFunction();
        expect(typeof theResult).toBe("function");
      });
    });