forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCases.test.js
More file actions
145 lines (143 loc) · 5.49 KB
/
TestCases.test.js
File metadata and controls
145 lines (143 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var should = require("should");
var path = require("path");
var fs = require("fs");
var vm = require("vm");
var Test = require("mocha/lib/test");
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;
})
};
});
[
{ name: "normal" },
{ name: "hot", plugins: [
new webpack.HotModuleReplacementPlugin()
]},
{ name: "devtool-eval", devtool: "eval" },
{ name: "devtool-source-map", devtool: "#@source-map" },
{ name: "minimized", plugins: [
new webpack.optimize.UglifyJsPlugin()
]},
{ name: "deduped", plugins: [
new webpack.optimize.DedupePlugin()
]},
{ name: "minimized-deduped", plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin()
]},
{ name: "optimized", plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin()
]},
{ name: "all-combined", devtool: "#@source-map", plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin()
]}
].forEach(function(config) {
describe(config.name, function() {
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.name, category.name, testName);
var options = {
context: casesPath,
entry: "./" + category.name + "/" + testName +"/index",
target: "async-node",
devtool: config.devtool,
output: {
pathinfo: true,
path: outputDirectory,
filename: "bundle.js"
},
resolve: {
modulesDirectories: ["web_modules", "node_modules"],
packageMains: ["webpack", "browser", "web", "browserify", ["jam", "main"], "main"],
extensions: ["", ".webpack.js", ".web.js", ".js"],
packageAlias: "browser"
},
resolveLoader: {
modulesDirectories: ["web_loaders", "web_modules", "node_loaders", "node_modules"],
packageMains: ["webpackLoader", "webLoader", "loader", "main"],
extensions: ["", ".webpack-loader.js", ".web-loader.js", ".loader.js", ".js"]
},
module: {
loaders: [
{ test: /\.json$/, loader: "json" },
{ test: /\.coffee$/, loader: "coffee" },
{ test: /\.jade$/, loader: "jade" }
]
},
plugins: (config.plugins || []).concat(
new webpack.dependencies.LabeledModulesPlugin()
)
};
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();
});
});
});
});
});
});
});
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;
}
}
});