forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.test.js
199 lines (189 loc) · 5.78 KB
/
Stats.test.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*globals describe it */
var should = require("should");
var path = require("path");
var fs = require("fs");
var mkdirp = require("mkdirp");
var webpack = require("../lib/webpack");
var base = path.join(__dirname, "statsCases");
var outputBase = path.join(__dirname, "js", "stats");
var tests = fs.readdirSync(base);
var Stats = require("../lib/Stats");
describe("Stats", function() {
tests.forEach(function(testName) {
it("should print correct stats for " + testName, function(done) {
var options = {
entry: "./index",
output: {
filename: "bundle.js"
}
};
if(fs.existsSync(path.join(base, testName, "webpack.config.js"))) {
options = require(path.join(base, testName, "webpack.config.js"));
}
(Array.isArray(options) ? options : [options]).forEach(function(options) {
options.context = path.join(base, testName);
options.output = options.output || {};
options.output.path = path.join(outputBase, testName);
});
var c = webpack(options);
var compilers = c.compilers ? c.compilers : [c];
compilers.forEach(function(c) {
var ifs = c.inputFileSystem;
c.inputFileSystem = Object.create(ifs);
c.inputFileSystem.readFile = function() {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
ifs.readFile.apply(ifs, args.concat([function(err, result) {
if(err) return callback(err);
callback(null, result.toString("utf-8").replace(/\r/g, ""));
}]));
};
c.apply(new webpack.optimize.OccurrenceOrderPlugin());
});
c.run(function(err, stats) {
if(err) return done(err);
if(/error$/.test(testName)) {
stats.hasErrors().should.be.equal(true);
} else if(stats.hasErrors()) {
done(new Error(stats.toJson().errors.join("\n\n")));
}
var toStringOptions = {
colors: false
};
var hasColorSetting = false;
if(typeof options.stats !== "undefined") {
toStringOptions = options.stats;
hasColorSetting = typeof options.stats.colors !== "undefined";
}
var actual = stats.toString(toStringOptions);
(typeof actual).should.be.eql("string");
if(!hasColorSetting) {
actual = actual
.replace(/\u001b\[[0-9;]*m/g, "")
.replace(/[0-9]+(\s?ms)/g, "X$1");
} else {
actual = actual
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, "<CLR=$1,BOLD>")
.replace(/\u001b\[1m/g, "<CLR=BOLD>")
.replace(/\u001b\[39m\u001b\[22m/g, "</CLR>")
.replace(/\u001b\[([0-9;]*)m/g, "<CLR=$1>")
.replace(/[0-9]+(<\/CLR>)?(\s?ms)/g, "X$1$2");
}
actual = actual
.replace(/\r\n?/g, "\n")
.replace(/[\t ]*Version:.+\n/g, "")
.replace(path.join(base, testName), "Xdir/" + testName);
var expected = fs.readFileSync(path.join(base, testName, "expected.txt"), "utf-8").replace(/\r/g, "");
if(actual !== expected) {
fs.writeFileSync(path.join(base, testName, "actual.txt"), actual, "utf-8");
} else if(fs.existsSync(path.join(base, testName, "actual.txt"))) {
fs.unlinkSync(path.join(base, testName, "actual.txt"));
}
actual.should.be.eql(expected);
done();
});
});
});
describe("Error Handling", function() {
describe("does have", function() {
it("hasErrors", function() {
var mockStats = new Stats({
errors: ['firstError'],
hash: '1234'
});
mockStats.hasErrors().should.be.ok;
});
it("hasWarnings", function() {
var mockStats = new Stats({
warnings: ['firstError'],
hash: '1234'
});
mockStats.hasWarnings().should.be.ok;
});
});
describe("does not have", function() {
it("hasErrors", function() {
var mockStats = new Stats({
errors: [],
hash: '1234'
});
mockStats.hasErrors().should.not.be.ok;
});
it("hasWarnings", function() {
var mockStats = new Stats({
warnings: [],
hash: '1234'
});
mockStats.hasWarnings().should.not.be.ok;
});
});
it("formatError handles string errors", function() {
var mockStats = new Stats({
errors: ['firstError'],
warnings: [],
assets: [],
chunks: [],
modules: [],
children: [],
hash: '1234',
mainTemplate: {
getPublicPath: function() {
return 'path';
}
}
});
var obj = mockStats.toJson();
obj.errors[0].should.be.equal('firstError');
});
});
describe("Presets", function() {
describe("presetToOptions", function() {
it("returns correct object with 'Normal'", function() {
Stats.presetToOptions("Normal").should.eql({
assets: false,
version: false,
timings: true,
hash: true,
chunks: true,
chunkModules: false,
errorDetails: true,
reasons: false,
colors: true
});
});
it("truthy values behave as 'normal'", function() {
var normalOpts = Stats.presetToOptions('normal');
Stats.presetToOptions("pizza").should.eql(normalOpts);
Stats.presetToOptions(true).should.eql(normalOpts);
Stats.presetToOptions(1).should.eql(normalOpts);
Stats.presetToOptions("verbose").should.not.eql(normalOpts);
Stats.presetToOptions(false).should.not.eql(normalOpts);
});
it("returns correct object with 'none'", function() {
Stats.presetToOptions("none").should.eql({
hash: false,
version: false,
timings: false,
assets: false,
chunks: false,
modules: false,
reasons: false,
children: false,
source: false,
errors: false,
errorDetails: false,
warnings: false,
publicPath: false
});
});
it("falsy values behave as 'none'", function() {
var noneOpts = Stats.presetToOptions('none');
Stats.presetToOptions("").should.eql(noneOpts);
Stats.presetToOptions(null).should.eql(noneOpts);
Stats.presetToOptions().should.eql(noneOpts);
Stats.presetToOptions(0).should.eql(noneOpts);
Stats.presetToOptions(false).should.eql(noneOpts);
});
});
});
});