Skip to content

Commit 9f826cb

Browse files
committed
fix(file-watcher): defer to default callback should fn property be absent from file watching object literals - fixes #643
1 parent c5e6309 commit 9f826cb

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/file-watcher.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ module.exports.plugin = function (bs) {
4444

4545
if (jsItem.objs.length) {
4646
jsItem.objs.forEach(function (item) {
47+
if (!_.isFunction(item.fn)) {
48+
item.fn = fn;
49+
}
4750
var watcher = watch(item.match, item.options || defaultWatchOptions, item.fn.bind(bs.publicInstance));
4851
if (!map[namespace]) {
4952
map[namespace] = {

test/specs/files/files.watching.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var merge = require("../../../lib/cli/cli-options").merge;
77

88
var events = require("events");
99
var path = require("path");
10+
var sinon = require("sinon");
1011
var assert = require("chai").assert;
1112

1213
var outpath = path.join(__dirname, "../../fixtures");
@@ -91,4 +92,42 @@ describe("File Watcher Module", function () {
9192
bs.watchers.core.watchers[0]._events.all("change", tempFile);
9293
});
9394
});
95+
it("should allow obj literal with match & options, but without callback fn", function (done) {
96+
97+
var tempFile = path.join(outpath, "watch-func.txt");
98+
99+
// assert: it works if it calls done
100+
var bs = browserSync.create();
101+
102+
bs.init({
103+
files: [
104+
{
105+
options: {
106+
ignoreInitial: true
107+
},
108+
match: tempFile
109+
}
110+
],
111+
ui: false,
112+
online: false,
113+
logSnippet: false,
114+
logLevel: "silent"
115+
}, function (err, bs) {
116+
117+
var spy = sinon.spy(bs.events, "emit");
118+
119+
bs.watchers.core.watchers[0]._events.all("change", tempFile);
120+
121+
var callArgs = spy.getCall(0).args;
122+
123+
assert.equal(callArgs[0], "file:changed");
124+
assert.equal(callArgs[1].basename, "watch-func.txt");
125+
assert.equal(callArgs[1].event, "change");
126+
assert.equal(callArgs[1].namespace, "core");
127+
128+
bs.cleanup();
129+
130+
done();
131+
});
132+
});
94133
});

0 commit comments

Comments
 (0)