node.js - 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 一起使用?
解决方案
推荐阅读
- javascript - 如何在 Javascript 中构造最终数组
- python - 如何在 Python 中选择正则表达式模式
- c# - 使用可为空和不可为空的日期值创建 Blazor 自定义日期选择器组件
- batch-file - 尝试从批处理文件中查找和加载信息后收到错误
- reactjs - 在两个 reducer 之间共享状态(React +Redux)
- python - 有没有更 Pythonic 的方式来处理字典?
- javascript - Angular 中的 document.querySelector
- java - 反序列化 JSON,其中键和值分别存储
- ruby-on-rails - Base64 编码参数的 Rails 路由约束
- python-3.x - 如何摆脱输出中的引号和括号?