webpack/test/ConfigTestCases.test.js

136 lines
5.2 KiB
JavaScript
Raw Normal View History

/* globals describe it */
2014-06-18 04:34:30 +08:00
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");
2016-09-07 15:46:27 +08:00
var Stats = require("../lib/Stats");
2014-06-18 04:34:30 +08:00
var webpack = require("../lib/webpack");
describe("ConfigTestCases", function() {
var casesPath = path.join(__dirname, "configCases");
var categories = fs.readdirSync(casesPath);
2014-06-18 04:34:30 +08:00
categories = categories.map(function(cat) {
return {
name: cat,
tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
return folder.indexOf("_") < 0;
}).sort().filter(function(testName) {
var testDirectory = path.join(casesPath, cat, testName);
var filterPath = path.join(testDirectory, "test.filter.js");
if(fs.existsSync(filterPath) && !require(filterPath)()) {
describe.skip(testName, function() {
it('filtered');
});
return false;
}
return true;
})
};
});
categories.forEach(function(category) {
describe(category.name, function() {
category.tests.forEach(function(testName) {
2014-06-18 04:34:30 +08:00
var suite = describe(testName, function() {});
it(testName + " should compile", function(done) {
2016-05-27 17:04:37 +08:00
this.timeout(30000);
var testDirectory = path.join(casesPath, category.name, testName);
2014-06-18 04:34:30 +08:00
var outputDirectory = path.join(__dirname, "js", "config", category.name, testName);
var options = require(path.join(testDirectory, "webpack.config.js"));
2014-08-22 19:51:24 +08:00
var optionsArr = [].concat(options);
optionsArr.forEach(function(options, idx) {
2014-06-19 05:02:33 +08:00
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;
2016-09-07 15:46:27 +08:00
if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true;
2014-06-19 05:02:33 +08:00
if(!options.output.filename) options.output.filename = "bundle" + idx + ".js";
if(!options.output.chunkFilename) options.output.chunkFilename = "[id].bundle" + idx + ".js";
2014-06-19 05:02:33 +08:00
});
2014-06-18 04:34:30 +08:00
webpack(options, function(err, stats) {
if(err) return done(err);
2016-09-07 15:46:27 +08:00
var statOptions = Stats.presetToOptions("verbose");
statOptions.colors = false;
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
2014-06-18 04:34:30 +08:00
var jsonStats = stats.toJson({
errorDetails: true
});
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
2014-08-05 00:08:52 +08:00
var exportedTests = 0;
2015-08-09 18:42:43 +08:00
2014-06-18 04:34:30 +08:00
function _it(title, fn) {
var test = new Test(title, fn);
suite.addTest(test);
2014-08-05 00:08:52 +08:00
exportedTests++;
2014-06-18 04:34:30 +08:00
return test;
}
2015-08-09 18:42:43 +08:00
2016-06-16 05:25:40 +08:00
var globalContext = {
console: console
};
2016-09-21 23:26:25 +08:00
function _require(currentDirectory, module) {
2016-06-16 05:25:40 +08:00
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
var fn;
2016-06-16 05:25:40 +08:00
var content;
if(Array.isArray(module)) {
2016-09-21 23:26:25 +08:00
var p = path.join(currentDirectory, module[0]);
2016-06-16 05:25:40 +08:00
content = module.map(function(p) {
2016-09-21 23:26:25 +08:00
var p = path.join(currentDirectory, p);
2016-06-16 05:25:40 +08:00
return fs.readFileSync(p, "utf-8");
}).join("\n");
} else {
2016-09-21 23:26:25 +08:00
var p = path.join(currentDirectory, module);
2016-06-16 05:25:40 +08:00
content = fs.readFileSync(p, "utf-8");
}
if(options.target === "web" || options.target === "webworker") {
2016-06-16 05:25:40 +08:00
fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, window) {" + content + "\n})", globalContext, p);
} else {
2016-06-16 05:25:40 +08:00
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it) {" + content + "\n})", p);
}
2015-08-09 18:42:43 +08:00
var module = {
exports: {}
};
2016-09-21 23:26:25 +08:00
fn.call(module.exports, _require.bind(null, path.dirname(p)), module, module.exports, path.dirname(p), p, _it, globalContext);
2014-06-18 04:34:30 +08:00
return module.exports;
} else if(testConfig.modules && module in testConfig.modules) {
return testConfig.modules[module];
2014-06-18 04:34:30 +08:00
} else return require(module);
}
2014-08-05 00:08:52 +08:00
var filesCount = 0;
2014-08-22 19:51:24 +08:00
var testConfig = {
2015-08-09 18:42:43 +08:00
findBundle: function(i, options) {
2014-08-22 19:51:24 +08:00
if(fs.existsSync(path.join(options.output.path, "bundle" + i + ".js"))) {
return "./bundle" + i + ".js";
}
}
};
try {
// try to load a test file
testConfig = require(path.join(testDirectory, "test.config.js"));
} catch(e) {}
if(testConfig.noTests) return process.nextTick(done);
2014-08-22 19:51:24 +08:00
for(var i = 0; i < optionsArr.length; i++) {
var bundlePath = testConfig.findBundle(i, optionsArr[i]);
2015-08-09 18:42:43 +08:00
if(bundlePath) {
2014-08-05 00:08:52 +08:00
filesCount++;
2016-09-21 23:26:25 +08:00
_require(outputDirectory, bundlePath);
2014-08-05 00:08:52 +08:00
}
}
2014-08-22 19:51:24 +08:00
// give a free pass to compilation that generated an error
if(!jsonStats.errors.length && filesCount !== optionsArr.length) return done(new Error("Should have found at least one bundle file per webpack config"));
2014-08-05 00:08:52 +08:00
if(exportedTests < filesCount) return done(new Error("No tests exported by test case"));
process.nextTick(done);
2014-06-18 04:34:30 +08:00
});
});
});
});
});
2014-08-22 19:51:24 +08:00
});