UNPKG

7.37 kBJavaScriptView Raw
1var should = require("should");
2var path = require("path");
3var TestHelper = require("./helpers/TestHelper");
4var Watchpack = require("../lib/watchpack");
5
6var fixtures = path.join(__dirname, "fixtures");
7var testHelper = new TestHelper(fixtures);
8
9describe("Watchpack", function() {
10 this.timeout(10000);
11 beforeEach(testHelper.before);
12 afterEach(testHelper.after);
13
14 it("should watch a single file", function(done) {
15 var w = new Watchpack({
16 aggregateTimeout: 1000
17 });
18 var changeEvents = 0;
19 w.on("change", function(file, mtime) {
20 file.should.be.eql(path.join(fixtures, "a"));
21 changeEvents++;
22 });
23 w.on("aggregated", function(changes) {
24 changes.should.be.eql([path.join(fixtures, "a")]);
25 changeEvents.should.be.eql(1);
26 w.close();
27 done();
28 });
29 w.watch([path.join(fixtures, "a")], []);
30 testHelper.tick(function() {
31 testHelper.file("a");
32 });
33 });
34
35 it("should watch multiple files", function(done) {
36 var w = new Watchpack({
37 aggregateTimeout: 1000
38 });
39 var changeEvents = [];
40 w.on("change", function(file, mtime) {
41 if(changeEvents[changeEvents.length-1] === file)
42 return;
43 changeEvents.push(file);
44 });
45 w.on("aggregated", function(changes) {
46 changes.sort().should.be.eql([path.join(fixtures, "a"), path.join(fixtures, "b")]);
47 changeEvents.should.be.eql([
48 path.join(fixtures, "a"),
49 path.join(fixtures, "b"),
50 path.join(fixtures, "a"),
51 path.join(fixtures, "b"),
52 path.join(fixtures, "a")
53 ]);
54 w.close();
55 done();
56 });
57 w.watch([path.join(fixtures, "a"), path.join(fixtures, "b")], []);
58 testHelper.tick(function() {
59 testHelper.file("a");
60 testHelper.tick(function() {
61 testHelper.file("b");
62 testHelper.tick(function() {
63 testHelper.file("a");
64 testHelper.tick(function() {
65 testHelper.file("b");
66 testHelper.tick(function() {
67 testHelper.file("a");
68 });
69 });
70 });
71 });
72 });
73 });
74
75 it("should watch a directory", function(done) {
76 var w = new Watchpack({
77 aggregateTimeout: 1000
78 });
79 var changeEvents = [];
80 w.on("change", function(file, mtime) {
81 if(changeEvents[changeEvents.length-1] === file)
82 return;
83 changeEvents.push(file);
84 });
85 w.on("aggregated", function(changes) {
86 changes.should.be.eql([path.join(fixtures, "dir")]);
87 changeEvents.should.be.eql([path.join(fixtures, "dir", "a")]);
88 w.close();
89 done();
90 });
91 testHelper.dir("dir");
92 testHelper.tick(function() {
93 w.watch([], [path.join(fixtures, "dir")]);
94 testHelper.tick(function() {
95 testHelper.file(path.join("dir", "a"));
96 });
97 });
98 });
99
100 it("should watch file in a sub directory", function(done) {
101 var w = new Watchpack({
102 aggregateTimeout: 1000
103 });
104 var changeEvents = [];
105 w.on("change", function(file, mtime) {
106 if(changeEvents[changeEvents.length-1] === file)
107 return;
108 changeEvents.push(file);
109 });
110 w.on("aggregated", function(changes) {
111 changes.should.be.eql([path.join(fixtures, "dir")]);
112 changeEvents.should.be.eql([path.join(fixtures, "dir", "sub", "a")]);
113 w.close();
114 done();
115 });
116 testHelper.dir("dir");
117 testHelper.dir(path.join("dir", "sub"));
118 testHelper.tick(function() {
119 w.watch([], [path.join(fixtures, "dir")]);
120 testHelper.tick(function() {
121 testHelper.file(path.join("dir", "sub", "a"));
122 });
123 });
124 });
125
126 it("should watch file in a sub sub directory", function(done) {
127 var w = new Watchpack({
128 aggregateTimeout: 1000
129 });
130 var changeEvents = [];
131 w.on("change", function(file, mtime) {
132 if(changeEvents[changeEvents.length-1] === file)
133 return;
134 changeEvents.push(file);
135 });
136 w.on("aggregated", function(changes) {
137 changes.should.be.eql([path.join(fixtures, "dir")]);
138 changeEvents.should.be.eql([path.join(fixtures, "dir", "sub", "sub", "a")]);
139 w.close();
140 done();
141 });
142 testHelper.dir("dir");
143 testHelper.dir(path.join("dir", "sub"));
144 testHelper.dir(path.join("dir", "sub", "sub"));
145 testHelper.tick(function() {
146 w.watch([], [path.join(fixtures, "dir")]);
147 testHelper.tick(function() {
148 testHelper.file(path.join("dir", "sub", "sub", "a"));
149 });
150 });
151 });
152
153 it("should detect a single change to future timestamps", function(done) {
154 var w = new Watchpack({
155 aggregateTimeout: 1000
156 });
157 var w2 = new Watchpack({
158 aggregateTimeout: 1000
159 });
160 w.on("change", function(file, mtime) {
161 throw new Error("should not report change event");
162 });
163 w.on("aggregated", function(changes) {
164 throw new Error("should not report aggregated event");
165 });
166 testHelper.file("a");
167 testHelper.tick(function() {
168 w2.watch([path.join(fixtures, "a")], []);
169 testHelper.tick(1000, function() { // wait for initial scan
170 testHelper.mtime("a", Date.now() + 1000000);
171 testHelper.tick(function() {
172 w.watch([path.join(fixtures, "a")], []);
173 testHelper.tick(function() {
174 testHelper.tick(function() {
175 w2.close();
176 w.close();
177 done();
178 });
179 });
180 });
181 });
182 });
183 });
184
185 it("should detect a past change to a file (timestamp)", function(done) {
186 var w = new Watchpack({
187 aggregateTimeout: 1000
188 });
189 var changeEvents = 0;
190 w.on("change", function(file, mtime) {
191 file.should.be.eql(path.join(fixtures, "a"));
192 changeEvents++;
193 });
194 w.on("aggregated", function(changes) {
195 changes.should.be.eql([path.join(fixtures, "a")]);
196 changeEvents.should.be.greaterThan(0);
197 w.close();
198 done();
199 });
200 var startTime = Date.now();
201 testHelper.tick(function() {
202 testHelper.file("a");
203 testHelper.tick(function() {
204 w.watch([path.join(fixtures, "a")], [], startTime);
205 });
206 });
207 });
208
209 it("should not detect a past change to a file (watched)", function(done) {
210 var w2 = new Watchpack();
211 var w = new Watchpack();
212 w.on("change", function(file, mtime) {
213 throw new Error("Should not be detected");
214 });
215 testHelper.tick(function() {
216 testHelper.file("b");
217 w2.watch([path.join(fixtures, "b")], [])
218 testHelper.tick(1000, function() { // wait for stable state
219 testHelper.file("a");
220 testHelper.tick(function() {
221 var startTime = Date.now();
222 testHelper.tick(function() {
223 w.watch([path.join(fixtures, "a")], [], startTime);
224 testHelper.tick(function() {
225 w.close();
226 w2.close();
227 done();
228 });
229 });
230 });
231 });
232 });
233 });
234
235 it("should detect a past change to a file (watched)", function(done) {
236 var w2 = new Watchpack();
237 var w = new Watchpack();
238 var changeEvents = 0;
239 w.on("change", function(file, mtime) {
240 file.should.be.eql(path.join(fixtures, "a"));
241 changeEvents++;
242 });
243 w.on("aggregated", function(changes) {
244 changes.should.be.eql([path.join(fixtures, "a")]);
245 changeEvents.should.be.eql(1);
246 w.close();
247 w2.close();
248 done();
249 });
250 testHelper.tick(function() {
251 testHelper.file("b");
252 w2.watch([path.join(fixtures, "b")], [])
253 testHelper.tick(function() {
254 var startTime = Date.now();
255 testHelper.tick(function() {
256 testHelper.file("a");
257 testHelper.tick(function() {
258 w.watch([path.join(fixtures, "a")], [], startTime);
259 });
260 });
261 });
262 });
263 });
264});