webpack/test/ProgressPlugin.test.js

198 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-05-15 19:19:53 +08:00
"use strict";
const _ = require("lodash");
2018-05-15 19:19:53 +08:00
const path = require("path");
2020-02-13 00:13:53 +08:00
const { createFsFromVolume, Volume } = require("memfs");
const captureStdio = require("./helpers/captureStdio");
2018-05-15 19:19:53 +08:00
let webpack;
describe("ProgressPlugin", function() {
let stderr;
2020-04-16 15:37:11 +08:00
let stdout;
beforeEach(() => {
stderr = captureStdio(process.stderr, true);
2020-04-16 15:37:11 +08:00
stdout = captureStdio(process.stdout, true);
2019-08-05 18:15:03 +08:00
webpack = require("..");
});
afterEach(() => {
stderr && stderr.restore();
2020-04-16 15:37:11 +08:00
stdout && stdout.restore();
});
it("should not contain NaN as a percentage when it is applied to MultiCompiler", () => {
2018-05-15 19:19:53 +08:00
const compiler = createMultiCompiler();
return RunCompilerAsync(compiler).then(() => {
expect(stderr.toString()).toContain("%");
expect(stderr.toString()).not.toContain("NaN");
2018-05-15 19:19:53 +08:00
});
});
2020-04-16 15:37:11 +08:00
it("should print profile information", () => {
const compiler = createSimpleCompiler({
profile: true
});
return RunCompilerAsync(compiler).then(() => {
const logs = getLogs(stderr.toString());
expect(logs).toContainEqual(
expect.stringMatching(
2020-04-25 06:49:30 +08:00
/\[webpack\.Progress\] \d+ ms module ids DeterministicModuleIdsPlugin\n$/
2020-04-16 15:37:11 +08:00
)
);
expect(logs).toContainEqual(
expect.stringMatching(
2020-04-25 06:49:30 +08:00
/\[webpack\.Progress\] \d+ ms(?: \(-\d+ ms\))? module ids\n$/
2020-04-16 15:37:11 +08:00
)
);
});
});
it("should not print lines longer than stderr.columns", () => {
const compiler = createSimpleCompiler();
2019-08-05 19:32:25 +08:00
process.stderr.columns = 36;
return RunCompilerAsync(compiler).then(() => {
const logs = getLogs(stderr.toString());
expect(logs.length).toBeGreaterThan(20);
2019-08-05 18:15:03 +08:00
logs.forEach(log => expect(log.length).toBeLessThanOrEqual(35));
expect(logs).toContain(
2019-08-05 18:15:03 +08:00
"75% ...optimization ...ChunksPlugin",
"trims each detail string equally"
);
expect(logs).toContain(
"10% ...ding ...ries ...cies ...ules",
2019-08-05 18:15:03 +08:00
"remove empty arguments and omit arguments when no space"
);
2019-11-12 21:34:19 +08:00
expect(logs).toContain("91% after chunk asset optimization");
2019-08-05 18:15:03 +08:00
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("3% normal module factory");
expect(logs).toContain("3% 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/));
});
});
});
2018-05-15 19:19:53 +08:00
const createMultiCompiler = () => {
const compiler = webpack([
{
context: path.join(__dirname, "fixtures"),
entry: "./a.js"
},
{
context: path.join(__dirname, "fixtures"),
entry: "./b.js"
}
]);
compiler.outputFileSystem = createFsFromVolume(new Volume());
new webpack.ProgressPlugin().apply(compiler);
2018-05-15 19:19:53 +08:00
return compiler;
};
const createSimpleCompiler = progressOptions => {
const compiler = webpack({
context: path.join(__dirname, "fixtures"),
2020-04-16 15:37:11 +08:00
entry: "./a.js",
infrastructureLogging: {
debug: /Progress/
}
});
compiler.outputFileSystem = createFsFromVolume(new Volume());
new webpack.ProgressPlugin({
activeModules: true,
...progressOptions
}).apply(compiler);
2018-05-15 19:19:53 +08:00
return compiler;
};
2018-05-15 19:19:53 +08:00
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) => {
2018-05-15 19:19:53 +08:00
compiler.run(err => {
if (err) {
reject(err);
2018-05-15 19:19:53 +08:00
} else {
resolve();
2018-05-15 19:19:53 +08:00
}
});
});