Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

339
Views
How to mock a specific method of a class whilst keeping the implementation of all other methods with jest when the class instance isn't accessible?

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!