Proxyquire is not working with TypeScript

Viewed 323

I have the following test

import proxyquire from "proxyquire";

const proxy = proxyquire.noCallThru();
const keytarStub: any = {
  setPassword: () => {
    console.log("MOCKED");
  },
};

const foo = proxy("./keytar.service.ts", {
  keytar: keytarStub,
});

describe("", () => {
  let service: any;

  let credentialsManagerServiceName: string = "accountdemo";

  beforeAll(() => {
    service = new foo.KeytarService(
      credentialsManagerServiceName,
    );
  });

  it("should", async () => {
    await service.save("DemoAcc", "eee");
    expect(true).toBeTruthy();
  });
});

and the class I want to mock

import keytar from "keytar";

export class KeytarService {
  private service: string;

  constructor(
    service: string,
  ) {
    this.service = service;
  }

  async save(account: string, password: string): Promise<void> {
    console.log(keytar);
    await keytar.setPassword(this.service, account, password);
  }
}

But the dependency does not get replaced.

I also included noCallThru() Is it possible for proxyquire not to work well with TypeScript?

0 Answers
Related