expect(jest.fn()).toHaveBeenCalled() fails even though the function has been called
Asked Answered
F

2

8

I'm trying to unit test a function that returns a promise. I'm having challenges verifying if the mocked functions are called. Here's what I've done.,

// codetotest.js
const { SomeModule, isSomething, isSomethingElse } = require("some-module");

exports.somefunction = (param1, param2)=> {
    const someModule = new SomeModule();

    someModule.someMethod("aaa", isSomething);
    someModule.someMethod("bbb", isSomethingElse);

    return (someModule.someOtherMethod(param1)
    .then(()=>{someModule.run(param2)}));
}

And this is the test file, the test says the mocked functions are not called, but I do see the console statement in the mock function is being displayed.

// codetotest.test.js
const { somefunction} = require("./codetotest.js");
const { SomeModule } = require("some-module");

jest.mock("some-module", () => {
    return {
        SomeModule: jest.fn().mockImplementation(() => {
            return {
                someMethod: jest.fn((param, fn) => { console.log("This prints!"); }),
                someOtherMethod: jest.fn((param) => { return Promise.resolve(() => { }) }),
                run: jest.fn((param) => { return Promise.resolve(() => { return []; }) })
            }
        })
    };
});

afterEach(() => {
    jest.resetAllMocks();
    jest.restoreAllMocks();
});

describe("Test codetotest.js", () => {
    it("somefunction() - success", async () => {
        const someModule = new SomeModule();

        let output = await somefunction("param1", "param2");

        expect(SomeModule).toHaveBeenCalled();
        expect(someModule.someMethod).toHaveBeenCalled(); // This fails

        await expect(someModule.someOtherMethod.mock.results[0]).resolves;
        expect(someModule.someOtherMethod).toHaveBeenCalled(); // This fails

        await expect(someModule.run.mocks.results[0]).resolves;
        expect(someModule.run).toHaveBeenCalled(); // This fails
    });
});

Appreciate any help/pointers.

Thank you.

P.S: I'm still a beginner when it comes to nodeJs development and unit testing.

Flam answered 14/5, 2021 at 17:15 Comment(0)
F
4

I spent quite some time on this and finally figured the instantiated mock class didn't return the mocked methods properly. This answer gave me a hint on where I was going wrong.

So accordingly, I had to change my test file as follows.,

// codetotest.test.js
const { somefunction} = require("./codetotest.js");
const { SomeModule } = require("some-module");

jest.mock("some-module", function() {
    return {
        SomeModule: jest.fn().mockImplementation(function() { // Arrow function cannot be used as constructor
            // Because I was not using the 'this' operator, my constructor always returned empty
            this.someMethod = jest.fn((param, fn) => { console.log("This prints!"); });
            this.someOtherMethod = jest.fn((param) => { return Promise.resolve(() => { }) });
            this.run = jest.fn((param) => { return Promise.resolve(() => { return []; }) });
            return {
                someMethod: this,someMethod,
                someOtherMethod: this.someOtherMethod,
                run: this.run
            }
        })
    };
});

afterEach(() => {
    jest.restoreAllMocks();
});

describe("Test codetotest.js", () => {
    it("somefunction() - success", async () => {
        await somefunction("param1", "param2");

        expect(SomeModule).toHaveBeenCalled();
        expect(SomeModule.mock.instances[0].someMethod).toHaveBeenCalled(); // This works
        expect(SomeModule.mock.instances[0].someOtherMethod).toHaveBeenCalled(); // This works
        expect(someModule.mock.instances[0].run).toHaveBeenCalled(); // This works
    });
});
Flam answered 16/5, 2021 at 9:48 Comment(0)
P
0

My case was a little different but it could help a bit some people coming here. The code was

jest.mock('@rollbar/react', () => ({
  useRollbar: jest.fn(() => {
    debug: jest.fn(),
    error: jest.fn(),
    info: jest.fn(),
    warning: jest.fn(),
  }),
}));

The problem with this code is that each time i call useRollbar() it will instantiate a new object, breaking the reference of the method implementations (cause in the test i have to call the hook to access the object to assert it has been call). Stated this, the solution is quite simple

const rollbarMethods = {
  debug: jest.fn(),
  error: jest.fn(),
  info: jest.fn(),
  warning: jest.fn(),
};

jest.mock('@rollbar/react', () => ({
  useRollbar: jest.fn(() => rollbarMethods),
}));

In this way the references returned by the module are always the same

Pizarro answered 16/9 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.