0

I want to test the check method which is a private method.

class Example {
  private check = async (x: string) => {
    //my code
  };
  public build() {
    const x: string = 'check';
    await this.check(x);
  }
}

Below is how i am testing the private method:

describe('Example', () => {
  it('should test the private method', () => {
    const handlePrivate = jest.spyOn(Example.prototype as any, 'check');

    expect(handlePrivate).toHaveBeenCalled();
  });
});

But an error is being thrown stating that

Cannot spy the check property because it is not a function; undefined given instead

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Sakshi
  • 1,464
  • 2
  • 8
  • 15

0 Answers0