2

Based on this question (How to mock instance methods of a class mocked with jest.mock?), how can a specific method be mocked whilst keeping the implementation of all other methods?

There's a similar question (Jest: How to mock one specific method of a class) but this only applies if the class instance is available outside it's calling class so this wouldn't work if the class instance was inside a constructor like in this question (How to mock a constructor instantiated class instance using jest?).

For example, the Logger class is mocked to have only method1 mocked but then method2 is missing, resulting in an error:

// Logger.ts
export default Logger() {
    constructor() {}
    method1() {
        return 'method1';
    }
    method2() {
        return 'method2';
    }
}

// Logger.test.ts
import Logger from './Logger';

jest.mock("./Logger", () => {
    return {
        default: class mockLogger {
            method1() {
                return 'mocked';
            }
        },
        __esModule: true,
    };
});

describe("Logger", () => {
    it("calls logger.method1() & logger.method2 on instantiation where only method1 is mocked", () => {
        const logger = new Logger(); // Assume this is called in the constructor of another object.

        expect(logger.method1()).toBe('mocked');
        expect(logger.method2()).toBe('method2'); // TypeError: logger.method2 is not a function.
    });
});

One solution is to extend the Logger class but this results in an undefined error as the Logger is already mocked:

// ...
jest.mock("./Logger", () => {
    return {
        default: class mockLogger extends Logger {
            override method1() {
                return 'mocked';
            }
        },
        __esModule: true,
    };
});
// ...
expect(logger.method2()).toBe('method2'); // TypeError: Cannot read property 'default' of undefined

Therefore, what could be the correct way to mock only method1 but keep method2's original implementation?

surajs02
  • 451
  • 7
  • 18

2 Answers2

2

You can use jest.spyOn and provide a mock implementation for method1.

// Logger.test.ts
import Logger from './Logger';

jest.spyOn(Logger.prototype, "method1").mockImplementation(() => "mocked")

describe("Logger", () => {
  it("calls method1 & method2 but only method1 is mocked", () => {
    const l = new Logger();
    expect(l.method1()).toBe("mocked");
    expect(l.method2()).toBe("method2");
  })
})

But in case you have many methods and you want to mock each one of them except one single method, then you can get the original implementation of this one single method using jest.requireActual.

// Logger.test.ts
import Logger from "./Logger";

const mockMethod1 = jest.fn().mockReturnValue("mocked");
const mockMethod3 = jest.fn().mockReturnValue("mocked");
const mockMethod4 = jest.fn().mockReturnValue("mocked");
const mockMethod5 = jest.fn().mockReturnValue("mocked");
jest.mock("./Logger", () =>
  jest.fn().mockImplementation(() => ({
    method1: mockMethod1,
    method2: jest.requireActual("./Logger").default.prototype.method2,
    method3: mockMethod3,
    method4: mockMethod4,
    method5: mockMethod5,
  }))
);

describe("Logger", () => {
  it("calls all methods but only method1 is mocked", () => {
    const l = new Logger();
    expect(l.method1()).toBe("mocked");
    expect(l.method2()).toBe("method2");
    expect(l.method3()).toBe("mocked");
    expect(l.method4()).toBe("mocked");
    expect(l.method5()).toBe("mocked");
  });
});

Note: You don't need to define an ES6 class for mocking, a constructor function also just works fine because ES6 classes are actually just syntactic sugar for constructor functions.

SSM
  • 2,855
  • 1
  • 3
  • 10
  • 1
    Nice, keeping the other methods by using prototype is much better than overwriting the methods, accepting this answer as it answers the question by making good use of jest – surajs02 May 18 '22 at 19:12
0

Mocking the prototype works:

describe("Logger", () => {
    it("calls logger.method1() & logger.method2 on instantiation where only method1 is mocked", () => {
        Logger.prototype.method1 = jest.fn(() => 'mocked');
        const logger = new Logger();

        expect(logger.method1()).toBe('mocked');
        expect(logger.method2()).toBe('method2');
    });
});

However, I'm not sure if this is the correct way to mock a specific method when the class instance isn't accessible so I'll leave the question open for while in case there are better solutions.

surajs02
  • 451
  • 7
  • 18