DEV Community

eggsys
eggsys

Posted on

Answer: Testing private method using spyOn and Jest

you can try jest.spyOn for get implementation of private method

Class

export default class Calculator {
  private Sum(a: number, b: number): number {
    let c = a + b;
    return c;
  }
}

Test

const handleErrorSpy = jest.spyOn(Calculator.prototype, 'Sum');
const getSumImplementation = handleErrorSpy.getMockImplementation();

expect(getSumImplementation(1, 2)).toEqual(3);

Top comments (0)