refactor(ES6): Compiler-caching.test.js

This commit is contained in:
Shubheksha Jalan 2017-01-18 13:53:20 +05:30
parent d9ac04380e
commit 280cbb7c8e
1 changed files with 92 additions and 100 deletions

View File

@ -1,11 +1,11 @@
var should = require("should");
var path = require("path");
var fs = require("fs");
"use strict";
var NodeEnvironmentPlugin = require("../lib/node/NodeEnvironmentPlugin");
var webpack = require("../");
var WebpackOptionsApply = require("../lib/WebpackOptionsApply");
var WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
const should = require("should");
const path = require("path");
const fs = require("fs");
const webpack = require("../");
const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
describe("Compiler (caching)", function() {
this.timeout(15000);
@ -17,13 +17,13 @@ describe("Compiler (caching)", function() {
options.output.path = "";
options.output.filename = "bundle.js";
options.output.pathinfo = true;
var logs = {
const logs = {
mkdirp: [],
writeFile: [],
};
var c = webpack(options);
var files = {};
const c = webpack(options);
const files = {};
c.outputFileSystem = {
join: path.join.bind(path),
mkdirp: function(path, callback) {
@ -36,18 +36,16 @@ describe("Compiler (caching)", function() {
callback();
}
};
c.plugin("compilation", function(compilation) {
compilation.bail = true;
});
c.plugin("compilation", (compilation) => compilation.bail = true);
var compilerIteration = 1;
let compilerIteration = 1;
function runCompiler(options, callback) {
if(typeof options === "function") {
callback = options;
options = {};
}
c.run(function(err, stats) {
c.run((err, stats) => {
if(err) throw err;
should.strictEqual(typeof stats, "object");
stats = stats.toJson({
@ -70,20 +68,20 @@ describe("Compiler (caching)", function() {
});
}
var postCompileCallbackStack = [];
const postCompileCallbackStack = [];
function addAfterCompileCallback(callback) {
postCompileCallbackStack.push(callback);
}
c.plugin("after-compile", function(stats, callback) {
c.plugin("after-compile", (stats, callback) => {
if(postCompileCallbackStack.length > 0) {
postCompileCallbackStack.shift().apply(this, arguments);
postCompileCallbackStack.shift(arguments);
}
callback()
})
callback();
});
runCompiler(callback);
@ -94,29 +92,23 @@ describe("Compiler (caching)", function() {
};
}
var tempFixturePath = path.join(__dirname, "fixtures", "temp-cache-fixture");
var aFilepath = path.join(tempFixturePath, 'a.js');
var cFilepath = path.join(tempFixturePath, 'c.js');
const tempFixturePath = path.join(__dirname, "fixtures", "temp-cache-fixture");
const aFilepath = path.join(tempFixturePath, "a.js");
const cFilepath = path.join(tempFixturePath, "c.js");
function cleanup() {
function ignoreENOENT(fn) {
try {
return fn();
} catch(e) {
if(e.code !== 'ENOENT') {
if(e.code !== "ENOENT") {
throw e;
}
}
}
ignoreENOENT(function() {
fs.unlinkSync(aFilepath);
});
ignoreENOENT(function() {
fs.unlinkSync(cFilepath);
});
ignoreENOENT(function() {
fs.rmdirSync(tempFixturePath);
});
ignoreENOENT(() => fs.unlinkSync(aFilepath));
ignoreENOENT(() => fs.unlinkSync(cFilepath));
ignoreENOENT(() => fs.rmdirSync(tempFixturePath));
}
before(cleanup);
after(cleanup);
@ -131,15 +123,15 @@ describe("Compiler (caching)", function() {
fs.rmdirSync(tempFixturePath);
}
} catch(e) {
if(e.code !== 'ENOENT') {
if(e.code !== "ENOENT") {
throw e;
}
}
// Copy over file since we'll be modifying some of them
// Copy over file since we"ll be modifying some of them
fs.mkdirSync(tempFixturePath);
fs.createReadStream(path.join(__dirname, "fixtures", 'a.js')).pipe(fs.createWriteStream(aFilepath));
fs.createReadStream(path.join(__dirname, "fixtures", 'c.js')).pipe(fs.createWriteStream(cFilepath));
fs.createReadStream(path.join(__dirname, "fixtures", "a.js")).pipe(fs.createWriteStream(aFilepath));
fs.createReadStream(path.join(__dirname, "fixtures", "c.js")).pipe(fs.createWriteStream(cFilepath));
return {
rootPath: tempFixturePath,
@ -148,32 +140,32 @@ describe("Compiler (caching)", function() {
};
}
it("should cache single file (with manual 1s wait) ", function(done) {
it("should cache single file (with manual 1s wait) ", (done) => {
var options = {};
var tempFixture = createTempFixture();
const options = {};
const tempFixture = createTempFixture();
var helper = compile("./temp-cache-fixture/c", options, function(stats, files) {
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
// Not cached the first time
stats.assets[0].name.should.be.exactly('bundle.js');
stats.assets[0].name.should.be.exactly("bundle.js");
stats.assets[0].emitted.should.be.exactly(true);
helper.runAgain(function(stats, files, iteration) {
helper.runAgain((stats, files, iteration) => {
// Cached the second run
stats.assets[0].name.should.be.exactly('bundle.js');
stats.assets[0].name.should.be.exactly("bundle.js");
stats.assets[0].emitted.should.be.exactly(false);
var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED');
const aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace("This is a", "This is a MODIFIED");
setTimeout(function() {
setTimeout(() => {
fs.writeFileSync(tempFixture.aFilepath, aContent);
helper.runAgain(function(stats, files, iteration) {
helper.runAgain((stats, files, iteration) => {
// Cached the third run
stats.assets[0].name.should.be.exactly('bundle.js');
stats.assets[0].name.should.be.exactly("bundle.js");
stats.assets[0].emitted.should.be.exactly(true);
done();
@ -184,36 +176,36 @@ describe("Compiler (caching)", function() {
});
});
it("should cache single file (even with no timeout) ", function(done) {
it("should cache single file (even with no timeout) ", (done) => {
var options = {};
var tempFixture = createTempFixture();
const options = {};
const tempFixture = createTempFixture();
var helper = compile("./temp-cache-fixture/c", options, function(stats, files) {
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
// Not cached the first time
stats.assets[0].name.should.be.exactly('bundle.js');
stats.assets[0].name.should.be.exactly("bundle.js");
stats.assets[0].emitted.should.be.exactly(true);
helper.runAgain(function(stats, files, iteration) {
helper.runAgain((stats, files, iteration) => {
// Cached the second run
stats.assets[0].name.should.be.exactly('bundle.js');
stats.assets[0].name.should.be.exactly("bundle.js");
stats.assets[0].emitted.should.be.exactly(false);
files['bundle.js'].should.containEql('"This is a"');
files["bundle.js"].should.containEql("This is a");
var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED');
const aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace("This is a", "This is a MODIFIED");
fs.writeFileSync(tempFixture.aFilepath, aContent);
helper.runAgain(function(stats, files, iteration) {
helper.runAgain((stats, files, iteration) => {
// Cached the third run
stats.assets[0].name.should.be.exactly('bundle.js');
stats.assets[0].name.should.be.exactly("bundle.js");
stats.assets[0].emitted.should.be.exactly(true);
files['bundle.js'].should.containEql('"This is a MODIFIED"');
files["bundle.js"].should.containEql("This is a MODIFIED");
done();
});
@ -221,43 +213,43 @@ describe("Compiler (caching)", function() {
});
});
it("should only build when modified (with manual 2s wait)", function(done) {
it("should only build when modified (with manual 2s wait)", (done) => {
var options = {};
var tempFixture = createTempFixture();
const options = {};
const tempFixture = createTempFixture();
var helper = compile("./temp-cache-fixture/c", options, function(stats, files) {
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
// Built the first time
stats.modules[0].name.should.containEql('a.js');
stats.modules[0].built.should.be.exactly(true, 'a.js should have been built');
stats.modules[0].name.should.containEql("a.js");
stats.modules[0].built.should.be.exactly(true, "a.js should have been built");
stats.modules[1].name.should.containEql('c.js');
stats.modules[1].built.should.be.exactly(true, 'c.js should have been built');
stats.modules[1].name.should.containEql("c.js");
stats.modules[1].built.should.be.exactly(true, "c.js should have been built");
setTimeout(function() {
helper.runAgain(function(stats, files, iteration) {
setTimeout(() => {
helper.runAgain((stats, files, iteration) => {
// Not built when cached the second run
stats.modules[0].name.should.containEql('a.js');
//stats.modules[0].built.should.be.exactly(false, 'a.js should not have built');
stats.modules[0].name.should.containEql("a.js");
//stats.modules[0].built.should.be.exactly(false, "a.js should not have built");
stats.modules[1].name.should.containEql('c.js');
//stats.modules[1].built.should.be.exactly(false, 'c.js should not have built');
stats.modules[1].name.should.containEql("c.js");
//stats.modules[1].built.should.be.exactly(false, "c.js should not have built");
var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED');
const aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace("This is a", "This is a MODIFIED");
setTimeout(function() {
setTimeout(() => {
fs.writeFileSync(tempFixture.aFilepath, aContent);
helper.runAgain(function(stats, files, iteration) {
helper.runAgain((stats, files, iteration) => {
// And only a.js built after it was modified
stats.modules[0].name.should.containEql('a.js');
stats.modules[0].built.should.be.exactly(true, 'a.js should have been built');
stats.modules[0].name.should.containEql("a.js");
stats.modules[0].built.should.be.exactly(true, "a.js should have been built");
stats.modules[1].name.should.containEql('c.js');
stats.modules[1].built.should.be.exactly(false, 'c.js should not have built');
stats.modules[1].name.should.containEql("c.js");
stats.modules[1].built.should.be.exactly(false, "c.js should not have built");
done();
});
@ -267,41 +259,41 @@ describe("Compiler (caching)", function() {
});
});
it("should build when modified (even with no timeout)", function(done) {
it("should build when modified (even with no timeout)", (done) => {
var options = {};
var tempFixture = createTempFixture();
const options = {};
const tempFixture = createTempFixture();
var helper = compile("./temp-cache-fixture/c", options, function(stats, files) {
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
// Built the first time
stats.modules[0].name.should.containEql('a.js');
stats.modules[0].built.should.be.exactly(true, 'a.js should have been built');
stats.modules[0].name.should.containEql("a.js");
stats.modules[0].built.should.be.exactly(true, "a.js should have been built");
stats.modules[1].name.should.containEql('c.js');
stats.modules[1].built.should.be.exactly(true, 'c.js should have been built');
stats.modules[1].name.should.containEql("c.js");
stats.modules[1].built.should.be.exactly(true, "c.js should have been built");
helper.runAgain(function(stats, files, iteration) {
helper.runAgain((stats, files, iteration) => {
// Not built when cached the second run
stats.modules[0].name.should.containEql('a.js');
//stats.modules[0].built.should.be.exactly(false, 'a.js should not have built');
stats.modules[0].name.should.containEql("a.js");
//stats.modules[0].built.should.be.exactly(false, "a.js should not have built");
stats.modules[1].name.should.containEql('c.js');
//stats.modules[1].built.should.be.exactly(false, 'c.js should not have built');
stats.modules[1].name.should.containEql("c.js");
//stats.modules[1].built.should.be.exactly(false, "c.js should not have built");
var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED');
const aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace("This is a", "This is a MODIFIED");
fs.writeFileSync(tempFixture.aFilepath, aContent);
helper.runAgain(function(stats, files, iteration) {
helper.runAgain((stats, files, iteration) => {
// And only a.js built after it was modified
stats.modules[0].name.should.containEql('a.js');
stats.modules[0].built.should.be.exactly(true, 'a.js should have been built');
stats.modules[0].name.should.containEql("a.js");
stats.modules[0].built.should.be.exactly(true, "a.js should have been built");
stats.modules[1].name.should.containEql('c.js');
//stats.modules[1].built.should.be.exactly(false, 'c.js should not have built');
stats.modules[1].name.should.containEql("c.js");
//stats.modules[1].built.should.be.exactly(false, "c.js should not have built");
done();
});