diff --git a/test/ConfigTestCases.test.js b/test/ConfigTestCases.test.js new file mode 100644 index 000000000..97ef0fa7f --- /dev/null +++ b/test/ConfigTestCases.test.js @@ -0,0 +1,67 @@ +var should = require("should"); +var path = require("path"); +var fs = require("fs"); +var vm = require("vm"); +var Test = require("mocha/lib/test"); +var checkArrayExpectation = require("./checkArrayExpectation"); + +var webpack = require("../lib/webpack"); + +describe("ConfigTestCases", function() { + var casesPath = path.join(__dirname, "configCases"); + var categories = fs.readdirSync(casesPath); + categories = categories.map(function(cat) { + return { + name: cat, + tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) { + return folder.indexOf("_") < 0; + }) + }; + }); + categories.forEach(function(category) { + describe(category.name, function() { + category.tests.forEach(function(testName) { + var suite = describe(testName, function() {}); + it(testName + " should compile", function(done) { + this.timeout(10000); + var testDirectory = path.join(casesPath, category.name, testName); + var outputDirectory = path.join(__dirname, "js", "config", category.name, testName); + var options = require(path.join(testDirectory, "webpack.config.js")); + if(!options.context) options.context = testDirectory; + if(!options.entry) options.entry = "./index.js"; + if(!options.target) options.target = "async-node"; + if(!options.output) options.output = {}; + if(!options.output.path) options.output.path = outputDirectory; + if(!options.output.filename) options.output.filename = "bundle.js"; + webpack(options, function(err, stats) { + if(err) return done(err); + var jsonStats = stats.toJson({ + errorDetails: true + }); + if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return; + if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return; + var exportedTest = 0; + function _it(title, fn) { + var test = new Test(title, fn); + suite.addTest(test); + exportedTest++; + return test; + } + function _require(module) { + if(module.substr(0, 2) === "./") { + var p = path.join(outputDirectory, module); + var fn = vm.runInThisContext("(function(require, module, exports, __dirname, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p); + var module = { exports: {} }; + fn.call(module.exports, _require, module, module.exports, outputDirectory, _it); + return module.exports; + } else return require(module); + } + _require("./bundle.js"); + if(exportedTest === 0) return done(new Error("No tests exported by test case")); + done(); + }); + }); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/TestCases.test.js b/test/TestCases.test.js index 29f88a091..603fbc7a4 100644 --- a/test/TestCases.test.js +++ b/test/TestCases.test.js @@ -3,6 +3,7 @@ var path = require("path"); var fs = require("fs"); var vm = require("vm"); var Test = require("mocha/lib/test"); +var checkArrayExpectation = require("./checkArrayExpectation"); var webpack = require("../lib/webpack"); @@ -121,26 +122,4 @@ describe("TestCases", function() { }); }); }); - function checkArrayExpectation(testDirectory, object, kind, upperCaseKind, done) { - var array = object[kind+"s"].slice().sort(); - if(kind === "warning") array = array.filter(function(item) { return !/from UglifyJs/.test(item); }); - if(fs.existsSync(path.join(testDirectory, kind+ "s.js"))) { - var expected = require(path.join(testDirectory, kind + "s.js")); - if(expected.length < array.length) - return done(new Error("More " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true; - else if(expected.length > array.length) - return done(new Error("Less " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true; - for(var i = 0; i < array.length; i++) { - if(Array.isArray(expected[i])) { - for(var j = 0; j < expected[i].length; j++) { - if(!expected[i][j].test(array[i])) - return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i][j].toString())), true; - } - } else if(!expected[i].test(array[i])) - return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i].toString())), true; - } - } else if(array.length > 0) { - return done(new Error(upperCaseKind + "s while compiling:\n\n" + array.join("\n\n"))), true; - } - } }); \ No newline at end of file diff --git a/test/checkArrayExpectation.js b/test/checkArrayExpectation.js new file mode 100644 index 000000000..a20bfe3fe --- /dev/null +++ b/test/checkArrayExpectation.js @@ -0,0 +1,25 @@ +var fs = require("fs"); +var path = require("path"); + +module.exports = function checkArrayExpectation(testDirectory, object, kind, upperCaseKind, done) { + var array = object[kind+"s"].slice().sort(); + if(kind === "warning") array = array.filter(function(item) { return !/from UglifyJs/.test(item); }); + if(fs.existsSync(path.join(testDirectory, kind+ "s.js"))) { + var expected = require(path.join(testDirectory, kind + "s.js")); + if(expected.length < array.length) + return done(new Error("More " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true; + else if(expected.length > array.length) + return done(new Error("Less " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true; + for(var i = 0; i < array.length; i++) { + if(Array.isArray(expected[i])) { + for(var j = 0; j < expected[i].length; j++) { + if(!expected[i][j].test(array[i])) + return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i][j].toString())), true; + } + } else if(!expected[i].test(array[i])) + return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i].toString())), true; + } + } else if(array.length > 0) { + return done(new Error(upperCaseKind + "s while compiling:\n\n" + array.join("\n\n"))), true; + } +} diff --git a/test/configCases/simple/empty-config/index.js b/test/configCases/simple/empty-config/index.js new file mode 100644 index 000000000..8a0a9b1f2 --- /dev/null +++ b/test/configCases/simple/empty-config/index.js @@ -0,0 +1,3 @@ +it("should compile and run the test", function() { + +}); \ No newline at end of file diff --git a/test/configCases/simple/empty-config/webpack.config.js b/test/configCases/simple/empty-config/webpack.config.js new file mode 100644 index 000000000..9d41f48e6 --- /dev/null +++ b/test/configCases/simple/empty-config/webpack.config.js @@ -0,0 +1,3 @@ +module.exports = { + +}; \ No newline at end of file