如何触发Chokidar事件programmaticaly

时间:2016-08-21 21:51:51

标签: javascript node.js typescript jasmine

我有一个班级,正在使用Chokidar,现在需要用茉莉花进行测试。这是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事件。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

观察程序实例继承自EventEmitter,因此您可以在其上调用.emit()(虽然我不确定如何使用TypeScript):

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