Vladimir Melekh Vladimir Melekh - 10 months ago 66
TypeScript Question

How to trigger Chokidar event programmaticaly

I have a class, which is using Chokidar and now need to test it with jasmine. This is method, where chokidar comes on board:

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

I want to have in my test something like:

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

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

The problem is that I don't know how to emulate Chokidar events. Any help is much appreciated.


The watcher instance inherits from EventEmitter, so you can call .emit() on it (although I'm not sure how/if that would work with TypeScript):

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