gpt4 book ai didi

javascript - 如何以编程方式触发 Chowkidar 事件

转载 作者:行者123 更新时间:2023-12-03 06:16:49 25 4
gpt4 key购买 nike

我有一门课,正在使用 Chokidar,现在需要用 jasmine 对其进行测试。这是 chokidar 加入的方法:

public watch(path: string) {
var watchOptions: chokidar.WatchOptions = <chokidar.WatchOptions> {
persistent: true
};
this.watcher = chokidar.watch(path, watchOptions);
this.watcher.on("add", (fileName: string, stats: fs.Stats) => {
this.sendNotifyAction(new NotifyAction(PathEvent.Add, fileName));
}).on("change", (fileName: string, stats: fs.Stats) => {
this.sendNotifyAction(new NotifyAction(PathEvent.Change, fileName));
})
}

我想在我的测试中进行如下操作:

    it("Should send notification, when internal directory is added", () => {
runs(() => {
flag = false;
pathWatch.watch(dirPath);

//TRIGGER chokidar on.add event??
//spyOn(pathWatch.watcher, "on").and.callFake((params) => {
// flag = true;
//});

});
waitsFor((): boolean => {
return flag;
}, "failure", 5000);

runs(() => {
var expectedNotifyAction = new NotifyAction(PathEvent.Add, notificationInternalDirPath);
expect(subscriber.processNotifyAction).toHaveBeenCalledWith(expectedNotifyAction);
});
});

问题是我不知道如何模拟 Chokidar 事件。非常感谢任何帮助。

最佳答案

观察者实例继承自 EventEmitter ,因此您可以对其调用 .emit() (尽管我不确定这如何/是否适用于 TypeScript):

pathWatch.watcher.emit('add', PATH, STATS);

关于javascript - 如何以编程方式触发 Chowkidar 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com