Skip to content

Commit f080ebe

Browse files
kylebuildsstuffKyle Truong
authored andcommitted
- Add tests to Compiler and Watching
1 parent 5678689 commit f080ebe

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

lib/Compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ module.exports = class Compiler extends Tapable {
226226
}
227227

228228
run(callback) {
229-
var self = this;
229+
const self = this;
230230
const startTime = Date.now();
231231

232232
self.applyPluginsAsync("before-run", self, err => {

test/Compiler.test.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const should = require("should");
55
const path = require("path");
6+
const sinon = require("sinon");
67

78
const webpack = require("../");
89
const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
@@ -163,4 +164,142 @@ describe("Compiler", () => {
163164
done();
164165
});
165166
});
167+
describe("methods", () => {
168+
let compiler;
169+
beforeEach(() => {
170+
compiler = webpack({
171+
entry: "./c",
172+
context: path.join(__dirname, "fixtures"),
173+
output: {
174+
path: "/",
175+
pathinfo: true,
176+
}
177+
});
178+
});
179+
describe("purgeInputFileSystem", () => {
180+
it("invokes purge() if inputFileSystem.purge", (done) => {
181+
const mockPurge = sinon.spy();
182+
compiler.inputFileSystem = {
183+
purge: mockPurge,
184+
};
185+
compiler.purgeInputFileSystem();
186+
mockPurge.callCount.should.be.exactly(1);
187+
done();
188+
});
189+
it("does NOT invoke purge() if !inputFileSystem.purge", (done) => {
190+
const mockPurge = sinon.spy();
191+
compiler.inputFileSystem = null;
192+
compiler.purgeInputFileSystem();
193+
mockPurge.callCount.should.be.exactly(0);
194+
done();
195+
});
196+
});
197+
describe("isChild", () => {
198+
it("returns booleanized this.parentCompilation", (done) => {
199+
compiler.parentCompilation = "stringyStringString";
200+
const response1 = compiler.isChild();
201+
response1.should.be.exactly(true);
202+
203+
compiler.parentCompilation = 123456789;
204+
const response2 = compiler.isChild();
205+
response2.should.be.exactly(true);
206+
207+
compiler.parentCompilation = { what: "I belong to an object" };
208+
const response3 = compiler.isChild();
209+
response3.should.be.exactly(true);
210+
211+
compiler.parentCompilation = ["Array", 123, true, null, [], () => {}];
212+
const response4 = compiler.isChild();
213+
response4.should.be.exactly(true);
214+
215+
compiler.parentCompilation = false;
216+
const response5 = compiler.isChild();
217+
response5.should.be.exactly(false);
218+
219+
compiler.parentCompilation = 0;
220+
const response6 = compiler.isChild();
221+
response6.should.be.exactly(false);
222+
223+
compiler.parentCompilation = null;
224+
const response7 = compiler.isChild();
225+
response7.should.be.exactly(false);
226+
227+
compiler.parentCompilation = "";
228+
const response8 = compiler.isChild();
229+
response8.should.be.exactly(false);
230+
231+
compiler.parentCompilation = NaN;
232+
const response9 = compiler.isChild();
233+
response9.should.be.exactly(false);
234+
done();
235+
});
236+
});
237+
});
238+
describe("Watching", () => {
239+
let compiler;
240+
beforeEach(() => {
241+
compiler = webpack({
242+
entry: "./c",
243+
context: path.join(__dirname, "fixtures"),
244+
output: {
245+
path: "/",
246+
pathinfo: true,
247+
}
248+
});
249+
});
250+
describe("constructor", () => {
251+
it("constructs Watching.watchOptions correctly when passed a number, string, or object for watchOptions", (done) => {
252+
const Watching1 = compiler.watch(1000, err => err);
253+
const Watching2 = compiler.watch({ aggregateTimeout: 1000 }, err => err);
254+
const Watching3 = compiler.watch("I am a string", err => err);
255+
Watching1.watchOptions.aggregateTimeout.should.equal(Watching2.watchOptions.aggregateTimeout);
256+
Watching3.watchOptions.aggregateTimeout.should.equal(200);
257+
done();
258+
});
259+
it("invokes compiler.readRecords", (done) => {
260+
compiler.readRecords = sinon.spy();
261+
compiler.watch(1000, err => err);
262+
compiler.readRecords.callCount.should.be.exactly(1);
263+
done();
264+
});
265+
});
266+
describe("_done", () => {
267+
it("invokes this.handler and turns this.running boolean to false when passed an error", (done) => {
268+
const mockHandler = sinon.spy();
269+
const Watching1 = compiler.watch(1000, mockHandler);
270+
Watching1.running.should.be.exactly(true);
271+
Watching1._done(Watching1.handler, false);
272+
mockHandler.callCount.should.be.exactly(1);
273+
Watching1.running.should.be.exactly(false);
274+
done();
275+
});
276+
});
277+
describe("invalidate", () => {
278+
it("pauses this.watcher and sets this.watcher to null if this.watcher is true", (done) => {
279+
const mockPause = sinon.spy();
280+
const Watching1 = compiler.watch(1000, err => err);
281+
Watching1.watcher = { pause: mockPause };
282+
Watching1.invalidate();
283+
mockPause.callCount.should.be.exactly(1);
284+
should(Watching1.watcher).be.exactly(null);
285+
done();
286+
});
287+
it("sets this.invalid to true if this.running is true, else this.invalid = false", (done) => {
288+
const Watching1 = compiler.watch(1000, err => err);
289+
Watching1.invalid = false;
290+
const response = Watching1.invalidate();
291+
Watching1.invalid.should.be.exactly(true);
292+
response.should.be.exactly(false);
293+
done();
294+
});
295+
it("invokes this._go() if !this.running", (done) => {
296+
const Watching1 = compiler.watch(1000, err => err);
297+
Watching1.running = false;
298+
Watching1._go = sinon.spy();
299+
Watching1.invalidate();
300+
Watching1._go.callCount.should.be.exactly(1);
301+
done();
302+
});
303+
});
304+
});
166305
});

0 commit comments

Comments
 (0)