webpack/test/TestCases.test.js

239 lines
6.9 KiB
JavaScript
Raw Normal View History

/* global describe, it*/
2017-01-18 23:27:35 +08:00
"use strict";
require("should");
2017-01-18 23:27:35 +08:00
const path = require("path");
const fs = require("fs");
const vm = require("vm");
const mkdirp = require("mkdirp");
2017-01-18 23:27:35 +08:00
const Test = require("mocha/lib/test");
const checkArrayExpectation = require("./checkArrayExpectation");
2017-01-18 23:27:35 +08:00
const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
const DEFAULT_OPTIMIZATIONS = {
removeAvailableModules: true,
removeEmptyChunks: true,
mergedDuplicateChunks: true,
flagIncludedChunks: true,
occurrenceOrder: true,
sideEffects: true,
providedExports: true,
usedExports: true,
noEmitOnErrors: false,
concatenateModules: false,
namedModules: false,
};
const NO_EMIT_ON_ERRORS_OPTIMIZATIONS = {
noEmitOnErrors: false
};
2017-01-18 23:27:35 +08:00
describe("TestCases", () => {
const casesPath = path.join(__dirname, "cases");
let categories = fs.readdirSync(casesPath);
categories = categories.map((cat) => {
return {
name: cat,
2017-01-18 23:27:35 +08:00
tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => folder.indexOf("_") < 0)
};
});
2015-08-09 18:42:43 +08:00
[{
name: "normal",
2017-05-10 19:15:14 +08:00
}, {
name: "production",
mode: "production"
}, {
name: "development",
mode: "development",
devtool: "none"
2015-08-09 18:42:43 +08:00
}, {
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",
mode: "production",
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",
mode: "production",
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",
mode: "production",
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",
mode: "production",
2015-08-09 18:42:43 +08:00
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(),
new webpack.NamedChunksPlugin()
2015-08-09 18:42:43 +08:00
]
2017-01-18 23:27:35 +08:00
}].forEach((config) => {
describe(config.name, () => {
categories.forEach((category) => {
2013-12-16 07:53:13 +08:00
describe(category.name, function() {
this.timeout(30000);
2017-01-18 23:27:35 +08:00
category.tests.filter((test) => {
const testDirectory = path.join(casesPath, category.name, test);
const filterPath = path.join(testDirectory, "test.filter.js");
2016-11-08 19:09:48 +08:00
if(fs.existsSync(filterPath) && !require(filterPath)(config)) {
2017-01-18 23:27:35 +08:00
describe.skip(test, () => it("filtered"));
2016-11-08 19:09:48 +08:00
return false;
2016-06-04 20:47:20 +08:00
}
return true;
2017-01-18 23:27:35 +08:00
}).forEach((testName) => {
const suite = describe(testName, () => {});
it(testName + " should compile", (done) => {
const testDirectory = path.join(casesPath, category.name, testName);
const outputDirectory = path.join(__dirname, "js", config.name, category.name, testName);
const 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,
mode: config.mode || "none",
optimization: config.mode ? NO_EMIT_ON_ERRORS_OPTIMIZATIONS : Object.assign({}, config.optimization, DEFAULT_OPTIMIZATIONS),
performance: {
hints: false
},
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"],
2017-11-29 23:34:49 +08:00
extensions: [".mjs", ".webpack.js", ".web.js", ".js", ".json"],
concord: true
},
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: {
2017-06-14 22:14:42 +08:00
rules: [{
2015-08-09 18:42:43 +08:00
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() {
2017-01-18 23:27:35 +08:00
this.plugin("compilation", (compilation) => {
["optimize", "optimize-modules-basic", "optimize-chunks-basic", "after-optimize-tree", "after-optimize-assets"].forEach((hook) => {
compilation.plugin(hook, () => compilation.checkConstraints());
});
});
})
};
2017-01-18 23:27:35 +08:00
webpack(options, (err, stats) => {
2013-12-16 07:53:13 +08:00
if(err) return done(err);
2017-01-18 23:27:35 +08:00
const statOptions = Stats.presetToOptions("verbose");
2016-09-07 15:46:27 +08:00
statOptions.colors = false;
mkdirp.sync(outputDirectory);
2016-09-07 15:46:27 +08:00
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
2017-01-18 23:27:35 +08:00
const 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;
2017-01-18 23:27:35 +08:00
let exportedTest = 0;
2015-08-09 18:42:43 +08:00
2013-12-16 07:53:13 +08:00
function _it(title, fn) {
2017-01-18 23:27:35 +08:00
const test = new Test(title, fn);
2013-12-16 07:53:13 +08:00
suite.addTest(test);
exportedTest++;
// WORKAROUND for a v8 bug
// Error objects retrain all scopes in the stacktrace
test._trace = test._trace.message;
2013-12-16 07:53:13 +08:00
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) === "./") {
2017-01-18 23:27:35 +08:00
const p = path.join(outputDirectory, module);
const fn = vm.runInThisContext("(function(require, module, exports, __dirname, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
const m = {
exports: {},
webpackTestSuiteModule: true
2015-08-09 18:42:43 +08:00
};
2017-01-18 23:27:35 +08:00
fn.call(m.exports, _require, m, m.exports, outputDirectory, _it);
return m.exports;
2013-12-16 07:53:13 +08:00
} else return require(module);
}
_require.webpackTestSuiteRequire = true;
2013-12-16 07:53:13 +08:00
_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
});