Skip to content

Commit 742208a

Browse files
committed
Tests for both new and old watch syntax
1 parent 4492c6e commit 742208a

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ coverage
88
*.orig
99
.idea
1010
sandbox
11-
test/out-fixtures/*
11+
test/out-fixtures/*
12+
test/watch-*.txt

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Gulp.prototype.watch = function (glob, opt, fn) {
3131
if (Array.isArray(fn) || typeof fn === 'string') {
3232
task = fn;
3333
fn = function () {
34-
this.start(task);
34+
inst.start(task);
3535
};
3636
}
3737
return vfs.watch(glob, opt, fn);

test/watch.js

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
3+
var gulp = require('../');
4+
var fs = require('fs');
5+
var path = require('path');
6+
7+
var should = require('should');
8+
require('mocha');
9+
10+
describe('gulp', function() {
11+
describe('watch()', function() {
12+
var tempFileContent = 'A test generated this file and it is safe to delete';
13+
14+
var writeTimeout = 125; // Wait for it to get to the filesystem
15+
var writeFile = function (name, content, cb) {
16+
fs.writeFile(name, content, cb);
17+
};
18+
var writeFileWait = function (name, content, cb) {
19+
setTimeout(function () {
20+
fs.writeFile(name, content, cb);
21+
}, writeTimeout);
22+
};
23+
24+
it('should call the function when file changes: no options', function(done) {
25+
26+
// arrange
27+
var tempFile = './test/watch-func.txt';
28+
writeFile(tempFile, tempFileContent, function () {
29+
30+
// assert: it works if it calls done
31+
gulp.watch(tempFile, function (evt) {
32+
should.exist(evt);
33+
should.exist(evt.path);
34+
should.exist(evt.type);
35+
evt.type.should.equal('changed');
36+
evt.path.should.equal(path.resolve(tempFile));
37+
done();
38+
});
39+
40+
// act: change file
41+
writeFileWait(tempFile, tempFileContent+' changed');
42+
});
43+
});
44+
45+
it('should call the function when file changes: w/ options', function(done) {
46+
// arrange
47+
var tempFile = './test/watch-func-options.txt';
48+
writeFile(tempFile, tempFileContent, function () {
49+
50+
// assert: it works if it calls done
51+
gulp.watch(tempFile, {debounceDelay:5}, function (evt) {
52+
should.exist(evt);
53+
should.exist(evt.path);
54+
should.exist(evt.type);
55+
evt.type.should.equal('changed');
56+
evt.path.should.equal(path.resolve(tempFile));
57+
done();
58+
});
59+
60+
// act: change file
61+
writeFileWait(tempFile, tempFileContent+' changed');
62+
});
63+
});
64+
65+
it('should run a task by name: no options', function(done) {
66+
// arrange
67+
var tempFile = './test/watch-task.txt';
68+
var taskName = 'run-task';
69+
var a = 0;
70+
var timeout = writeTimeout * 2.5;
71+
72+
writeFile(tempFile, tempFileContent, function () {
73+
74+
gulp.task(taskName, function () {
75+
a++;
76+
});
77+
78+
// assert
79+
setTimeout(function () {
80+
a.should.equal(1);
81+
82+
gulp.reset();
83+
done();
84+
}, timeout);
85+
86+
// it works if it calls the task
87+
gulp.watch(tempFile, taskName);
88+
89+
// act: change file
90+
writeFileWait(tempFile, tempFileContent+' changed');
91+
});
92+
});
93+
94+
it('should run many tasks: w/ options', function(done) {
95+
// arrange
96+
var tempFile = './test/watch-task-options.txt';
97+
var task1 = 'task1';
98+
var task2 = 'task2';
99+
var a = 0;
100+
var timeout = writeTimeout * 2.5;
101+
102+
writeFile(tempFile, tempFileContent, function () {
103+
104+
gulp.task(task1, function () {
105+
a++;
106+
});
107+
gulp.task(task2, function () {
108+
a += 10;
109+
});
110+
111+
// assert
112+
setTimeout(function () {
113+
a.should.equal(11); // task1 and task2
114+
115+
gulp.reset();
116+
done();
117+
}, timeout);
118+
119+
// it works if it calls the task
120+
gulp.watch(tempFile, {debounceDelay:timeout/2}, [task1,task2]);
121+
122+
// act: change file
123+
writeFileWait(tempFile, tempFileContent+' changed');
124+
});
125+
});
126+
127+
});
128+
});

0 commit comments

Comments
 (0)