Spying Private Functions

In case you are only exposing the private methods to stub / spy on with SinonJS, you can keep the method private and do the following instead:

export class SomeClassYouWantToTest {
  ...
  private someMethod(...) { ... }
}
// in test
  ...
  const foo = new SomeClassYouWantToTest();
  const someMethodSpy = sinon.spy(foo, "someMethod" as keyof SomeClassYouWantToTest);
  ...

In TypeScript, private functions and properties are just a suggestion for the type checker. The underlying functions and properties are still actually accessible, so we use a cast to bypass the typechecker.


Backlinks