4

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?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Georgian Stan
  • 1,865
  • 3
  • 13
  • 27

0 Answers0