webpack/test/TestCases.test.js

200 lines
5.9 KiB
JavaScript
Raw Normal View History

var should = require("should");
var path = require("path");
var fs = require("fs");
var vm = require("vm");
2013-12-16 06:54:34 +08:00
var Test = require("mocha/lib/test");
2014-06-18 04:34:30 +08:00
var checkArrayExpectation = require("./checkArrayExpectation");
2016-09-07 15:46:27 +08:00
var Stats = require("../lib/Stats");
var webpack = require("../lib/webpack");
describe("TestCases", function() {
var casesPath = path.join(__dirname, "cases");
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;
})
};
});
2015-08-09 18:42:43 +08:00
[{
name: "normal"
}, {
name: "hot",
plugins: [
new webpack.HotModuleReplacementPlugin()
2015-08-09 18:42:43 +08:00
]
}, {
name: "hot-multi-step",
plugins: [
2015-01-31 23:27:05 +08:00
new webpack.HotModuleReplacementPlugin({
multiStep: true
})
]
2015-08-09 18:42:43 +08:00
}, {
name: "devtool-eval",
devtool: "eval"
}, {
name: "devtool-eval-named-modules",
devtool: "eval",
plugins: [
new webpack.NamedModulesPlugin()
2015-08-09 18:42:43 +08:00
]
}, {
name: "devtool-eval-source-map",
devtool: "#eval-source-map"
}, {
name: "devtool-inline-source-map",
devtool: "inline-source-map"
}, {
name: "devtool-source-map",
devtool: "#@source-map"
}, {
name: "devtool-cheap-inline-source-map",
devtool: "cheap-inline-source-map"
}, {
name: "devtool-cheap-eval-source-map",
devtool: "cheap-eval-source-map"
}, {
name: "devtool-cheap-eval-module-source-map",
devtool: "cheap-eval-module-source-map"
}, {
name: "devtool-cheap-source-map",
devtool: "cheap-source-map"
}, {
name: "minimized",
2016-06-04 20:47:20 +08:00
minimize: true,
2015-08-09 18:42:43 +08:00
plugins: [
new webpack.optimize.UglifyJsPlugin({
sourceMap: false
})
2015-08-09 18:42:43 +08:00
]
}, {
name: "minimized-source-map",
devtool: "eval-cheap-module-source-map",
2016-06-04 20:47:20 +08:00
minimize: true,
2015-08-09 18:42:43 +08:00
plugins: [
new webpack.optimize.UglifyJsPlugin()
2015-08-09 18:42:43 +08:00
]
}, {
name: "minimized-hashed-modules",
2016-06-04 20:47:20 +08:00
minimize: true,
2015-08-09 18:42:43 +08:00
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new webpack.HashedModuleIdsPlugin()
2015-08-09 18:42:43 +08:00
]
}, {
name: "all-combined",
devtool: "#@source-map",
2016-06-04 20:47:20 +08:00
minimize: true,
2015-08-09 18:42:43 +08:00
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.NamedModulesPlugin()
2015-08-09 18:42:43 +08:00
]
}].forEach(function(config) {
2013-12-16 07:53:13 +08:00
describe(config.name, function() {
categories.forEach(function(category) {
describe(category.name, function() {
this.timeout(30000);
category.tests.filter(function(test) {
2016-06-04 20:47:20 +08:00
var testDirectory = path.join(casesPath, category.name, test);
var filterPath = path.join(testDirectory, "test.filter.js");
2016-11-08 19:09:48 +08:00
if(fs.existsSync(filterPath) && !require(filterPath)(config)) {
2016-11-08 19:42:49 +08:00
describe.skip(test, function() {
it('filtered');
2016-11-08 19:42:49 +08:00
});
2016-11-08 19:09:48 +08:00
return false;
2016-06-04 20:47:20 +08:00
}
return true;
}).forEach(function(testName) {
2013-12-16 07:53:13 +08:00
var suite = describe(testName, function() {});
2014-01-31 18:58:20 +08:00
it(testName + " should compile", function(done) {
2013-12-16 07:53:13 +08:00
var testDirectory = path.join(casesPath, category.name, testName);
var outputDirectory = path.join(__dirname, "js", config.name, category.name, testName);
var options = {
2013-12-16 07:53:13 +08:00
context: casesPath,
2015-08-09 18:42:43 +08:00
entry: "./" + category.name + "/" + testName + "/index",
2013-12-16 07:53:13 +08:00
target: "async-node",
devtool: config.devtool,
2013-12-16 07:53:13 +08:00
output: {
pathinfo: true,
path: outputDirectory,
filename: "bundle.js"
},
resolve: {
modules: ["web_modules", "node_modules"],
mainFields: ["webpack", "browser", "web", "browserify", ["jam", "main"], "main"],
aliasFields: ["browser"],
extensions: [".webpack.js", ".web.js", ".js", ".json"]
},
resolveLoader: {
modules: ["web_loaders", "web_modules", "node_loaders", "node_modules"],
mainFields: ["webpackLoader", "webLoader", "loader", "main"],
extensions: [".webpack-loader.js", ".web-loader.js", ".loader.js", ".js"]
},
2013-12-16 07:53:13 +08:00
module: {
2015-08-09 18:42:43 +08:00
loaders: [{
test: /\.coffee$/,
2016-01-27 00:56:44 +08:00
loader: "coffee-loader"
2015-08-09 18:42:43 +08:00
}, {
test: /\.jade$/,
2016-01-27 00:56:44 +08:00
loader: "jade-loader"
2015-08-09 18:42:43 +08:00
}]
},
plugins: (config.plugins || []).concat(
function() {
this.plugin("compilation", function(compilation) {
["optimize", "optimize-modules-basic", "optimize-chunks-basic", "after-optimize-tree", "after-optimize-assets"].forEach(function(hook) {
compilation.plugin(hook, function() {
compilation.checkConstraints();
});
});
});
}
)
};
webpack(options, function(err, stats) {
2013-12-16 07:53:13 +08:00
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");
var jsonStats = stats.toJson({
errorDetails: true
});
2013-12-16 07:53:13 +08:00
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
var exportedTest = 0;
2015-08-09 18:42:43 +08:00
2013-12-16 07:53:13 +08:00
function _it(title, fn) {
var test = new Test(title, fn);
suite.addTest(test);
exportedTest++;
return test;
}
2015-08-09 18:42:43 +08:00
2013-12-16 07:53:13 +08:00
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);
2015-08-09 18:42:43 +08:00
var module = {
exports: {}
};
2013-12-16 07:53:13 +08:00
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();
});
});
});
});
});
});
});
2015-01-31 23:27:05 +08:00
});