forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressPlugin.test.js
254 lines (216 loc) · 6.64 KB
/
ProgressPlugin.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"use strict";
const _ = require("lodash");
const path = require("path");
const { createFsFromVolume, Volume } = require("memfs");
const webpack = require("..");
const captureStdio = require("./helpers/captureStdio");
const createMultiCompiler = (progressOptions, configOptions) => {
const compiler = webpack(
Object.assign(
[
{
context: path.join(__dirname, "fixtures"),
entry: "./a.js"
},
{
context: path.join(__dirname, "fixtures"),
entry: "./b.js"
}
],
configOptions
)
);
compiler.outputFileSystem = createFsFromVolume(new Volume());
new webpack.ProgressPlugin(progressOptions).apply(compiler);
return compiler;
};
const createSimpleCompiler = progressOptions => {
const compiler = webpack({
context: path.join(__dirname, "fixtures"),
entry: "./a.js",
infrastructureLogging: {
debug: /Progress/
}
});
compiler.outputFileSystem = createFsFromVolume(new Volume());
new webpack.ProgressPlugin({
activeModules: true,
...progressOptions
}).apply(compiler);
return compiler;
};
const createSimpleCompilerWithCustomHandler = options => {
const compiler = webpack({
context: path.join(__dirname, "fixtures"),
entry: "./a.js"
});
compiler.outputFileSystem = createFsFromVolume(new Volume());
const logger = compiler.getInfrastructureLogger("custom test logger");
new webpack.ProgressPlugin({
activeModules: true,
...options,
handler: (...args) => logger.status(args)
}).apply(compiler);
return compiler;
};
const getLogs = logsStr => logsStr.split(/\r/).filter(v => !(v === " "));
const RunCompilerAsync = compiler =>
new Promise((resolve, reject) => {
compiler.run(err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
describe("ProgressPlugin", function () {
let stderr;
let stdout;
beforeEach(() => {
stderr = captureStdio(process.stderr, true);
stdout = captureStdio(process.stdout, true);
});
afterEach(() => {
stderr && stderr.restore();
stdout && stdout.restore();
});
const nanTest = createCompiler => () => {
const compiler = createCompiler();
return RunCompilerAsync(compiler).then(() => {
expect(stderr.toString()).toContain("%");
expect(stderr.toString()).not.toContain("NaN");
});
};
it(
"should not contain NaN as a percentage when it is applied to Compiler",
nanTest(createSimpleCompiler)
);
it(
"should not contain NaN as a percentage when it is applied to MultiCompiler",
nanTest(createMultiCompiler)
);
it(
"should not contain NaN as a percentage when it is applied to MultiCompiler (paralellism: 1)",
nanTest(() => createMultiCompiler(undefined, { parallelism: 1 }))
);
it("should print profile information", () => {
const compiler = createSimpleCompiler({
profile: true
});
return RunCompilerAsync(compiler).then(() => {
const logs = getLogs(stderr.toString());
expect(logs).toContainEqual(
expect.stringMatching(
/\[webpack\.Progress\] {2}| {2}| \d+ ms module ids > DeterministicModuleIdsPlugin\n$/
)
);
expect(logs).toContainEqual(
expect.stringMatching(
/\[webpack\.Progress\] {2}| \d+ ms building > \.\.\. entries \.\.\. dependencies \.\.\. modules\n$/
)
);
expect(logs).toContainEqual(
expect.stringMatching(/\[webpack\.Progress\] \d+ ms building\n$/)
);
expect(logs).toContainEqual(
expect.stringMatching(
/\[webpack\.Progress\] {2}| \d+ ms sealing > module ids\n$/
)
);
expect(logs).toContainEqual(
expect.stringMatching(/\[webpack\.Progress\] \d+ ms sealing\n$/)
);
});
});
const monotonicTest = createCompiler => () => {
const handlerCalls = [];
const compiler = createCompiler({
handler: (p, ...args) => {
handlerCalls.push({ value: p, text: `${p}% ${args.join(" ")}` });
}
});
return RunCompilerAsync(compiler).then(() => {
let lastLine = handlerCalls[0];
for (const line of handlerCalls) {
if (line.value < lastLine.value) {
throw new Error(
`Progress value is not monotonic increasing:\n${lastLine.text}\n${line.text}`
);
}
lastLine = line;
}
});
};
it(
"should have monotonic increasing progress",
monotonicTest(createSimpleCompiler)
);
it(
"should have monotonic increasing progress (multi compiler)",
monotonicTest(createMultiCompiler)
);
it(
"should have monotonic increasing progress (multi compiler, parallelism)",
monotonicTest(o => createMultiCompiler(o, { parallelism: 1 }))
);
it("should not print lines longer than stderr.columns", () => {
const compiler = createSimpleCompiler();
process.stderr.columns = 36;
return RunCompilerAsync(compiler).then(() => {
const logs = getLogs(stderr.toString());
expect(logs.length).toBeGreaterThan(20);
logs.forEach(log => expect(log.length).toBeLessThanOrEqual(35));
expect(logs).toContain(
"75% sealing ...mization ...nsPlugin",
"trims each detail string equally"
);
expect(logs).toContain("92% sealing asset processing");
expect(logs).toContain("100%");
});
});
it("should handle when stderr.columns is undefined", () => {
const compiler = createSimpleCompiler();
process.stderr.columns = undefined;
return RunCompilerAsync(compiler).then(() => {
const logs = getLogs(stderr.toString());
expect(logs.length).toBeGreaterThan(20);
expect(_.maxBy(logs, "length").length).toBeGreaterThan(50);
});
});
it("should contain the new compiler hooks", () => {
const compiler = createSimpleCompiler();
process.stderr.columns = undefined;
return RunCompilerAsync(compiler).then(() => {
const logs = getLogs(stderr.toString());
expect(logs).toContain("4% setup normal module factory");
expect(logs).toContain("5% setup context module factory");
});
});
it("should display all type of percentage when it is applied to SingleCompiler", () => {
const compiler = createSimpleCompiler({
entries: true,
modules: true,
dependencies: true,
activeModules: true
});
return RunCompilerAsync(compiler).then(() => {
const logs = stderr.toString();
expect(logs).toEqual(expect.stringMatching(/\d+\/\d+ entries/));
expect(logs).toEqual(expect.stringMatching(/\d+\/\d+ dependencies/));
expect(logs).toEqual(expect.stringMatching(/\d+\/\d+ modules/));
expect(logs).toEqual(expect.stringMatching(/\d+ active/));
});
});
it("should get the custom handler text from the log", () => {
const compiler = createSimpleCompilerWithCustomHandler();
return RunCompilerAsync(compiler).then(() => {
const logs = stderr.toString();
expect(logs).toEqual(
expect.stringMatching(/\d+\/\d+ [custom test logger]/)
);
expect(logs).toEqual(expect.stringMatching(/\d+ active/));
expect(logs).toEqual(expect.stringMatching(/\d+\/\d+ modules/));
});
});
});