2016-08-21 7 views
0

私はChokidarを使用しているクラスを持っており、今はジャスミンでそれをテストする必要があります。問題は、私はChokidarイベントをエミュレートする方法がわからないということですChokidarイベントをトリガーする方法

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

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

私のような私のテストの何かを持っているしたい:これはchokidarは、ボード上で来る方法、です。どんな助けも大歓迎です。

+0

は、なぜ単にファイルに書き込み、追加しませんnode.jsファイルAPIを持つファイルであれば、chokidar – qballer

+0

が起動するはずですが、これはちょっと面倒ですがchokidarイベントをエミュレートする方法が面白いです –

+2

chokidarのテストhttps://github.com/paulmillr /chokidar/blob/master/test.js#L162 – qballer

答えて

1

ウォッチャーインスタンスがEventEmitterから継承するので、(私はそれが活字体で動作するかどうか/わからないが)、あなたはそれに.emit()を呼び出すことができます。

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

ありがとう、それは私のために働く! –

関連する問題