首页 > 解决方案 > Proxyquire 不适用于 TypeScript

问题描述

我有以下测试

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 一起使用?

标签: node.jstypescriptjestjsproxyquire

解决方案


推荐阅读