3

我有以下测试

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();
  });
});

和我想模拟的课程

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);
  }
}

但是依赖并没有被替换。

我还包括noCallThru() proxyquire是否 可能无法与 TypeScript 一起使用?

4

0 回答 0