Skip to content

Instantly share code, notes, and snippets.

@lamngockhuong
Last active August 12, 2024 16:23
Show Gist options
  • Save lamngockhuong/2f5575950ba1b1c42c9f98a300a250e4 to your computer and use it in GitHub Desktop.
Save lamngockhuong/2f5575950ba1b1c42c9f98a300a250e4 to your computer and use it in GitHub Desktop.
Test & mock private function by jest
export class Test {
  constructor() {}

  private testPri(a: string): string {
    return a + 'test';
  }
}
describe("Test", () => {
  
  it("test private function", async () => {
    const test = new Test();
    const r = test["testPri"]("a");
    expect(r).toBe("atest");
  });
  it("mock private function", async () => {
    const test = new Test();
    jest.spyOn(test as any, "testPri").mockReturnValue("token");
    const r = test["testPri"]("a");
    expect(r).toBe("token");
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment