Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ class BundleAnalyzerPlugin {
setImmediate(async () => {
try {
await Promise.all(actions.map((action) => action()));
} finally {
callback();
} catch (err) {
callback(err);
}
});
} else {
Expand Down
106 changes: 76 additions & 30 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const path = require("node:path");
const url = require("node:url");
const puppeteer = require("puppeteer");
const webpack = require("webpack");
const BundleAnalyzerPlugin = require("../lib/BundleAnalyzerPlugin");
const { isZstdSupported } = require("../src/sizeUtils");
const {
Expand Down Expand Up @@ -95,26 +96,30 @@
recursive: true,
});
});

Check failure on line 99 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const NODE_MAJOR = Number.parseInt(process.versions.node.split(".")[0], 10);
const SKIP_WEBPACK_4 = NODE_MAJOR >= 20;

Check failure on line 102 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
if (!SKIP_WEBPACK_4) {
forEachWebpackVersion(["4.44.2"], ({ it, webpackCompile }) => {
it("should support webpack config with custom `jsonpFunction` name", async () => {
const config = makeWebpackConfig({
multipleChunks: true,
});

forEachWebpackVersion(["4.44.2"], ({ it, webpackCompile }) => {
// Webpack 5 doesn't support `jsonpFunction` option
it("should support webpack config with custom `jsonpFunction` name", async () => {
const config = makeWebpackConfig({
multipleChunks: true,
});

config.output.jsonpFunction = "somethingCompletelyDifferent";
config.output.jsonpFunction = "somethingCompletelyDifferent";

await webpackCompile(config);
await webpackCompile(config);

await expectValidReport({
parsedSize: 1349,
gzipSize: 358,
await expectValidReport({
parsedSize: 1349,
gzipSize: 358,
});
});
});
});
}

/* eslint jest/no-standalone-expect: ["error", { additionalTestBlockFunctions: ["forEachWebpackVersion"] }] */
/* eslint jest/no-standalone-expect: ["error", { additionalTestBlockFunctions: ["forEachWebpackVersion", "runTest"] }] */
forEachWebpackVersion(({ it, webpackCompile }) => {
it("should allow to generate json report", async () => {
const config = makeWebpackConfig({
Expand Down Expand Up @@ -182,7 +187,8 @@
});

describe("reportTitle", () => {
it("should have a sensible default", async () => {
const runTest = SKIP_WEBPACK_4 ? it.skip : it;
runTest("should have a sensible default", async () => {
const config = makeWebpackConfig();
await webpackCompile(config, "4.44.2");
const generatedReportTitle = await getTitleFromReport();
Expand All @@ -191,7 +197,7 @@
);
});

it("should support a string value", async () => {
runTest("should support a string value", async () => {
const reportTitle = "A string report title";
const config = makeWebpackConfig({
analyzerOpts: {
Expand All @@ -203,7 +209,7 @@
expect(generatedReportTitle).toBe(reportTitle);
});

it("should support a function value", async () => {
runTest("should support a function value", async () => {
const reportTitleResult = "A string report title";
const config = makeWebpackConfig({
analyzerOpts: {
Expand All @@ -215,7 +221,7 @@
expect(generatedReportTitle).toBe(reportTitleResult);
});

it("should propagate an error in a function", async () => {
runTest("should log an error when reportTitle throws", async () => {
const reportTitleError = new Error("test");
const config = makeWebpackConfig({
analyzerOpts: {
Expand All @@ -225,33 +231,36 @@
},
});

let error = null;
try {
await webpackCompile(config, "4.44.2");
} catch (err) {
error = err;
}
const errorSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
await webpackCompile(config, "4.44.2");

Check failure on line 238 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `········`
expect(errorSpy).toHaveBeenCalledWith(
expect.stringContaining("action failed: test"),
);

expect(error).toBe(reportTitleError);
errorSpy.mockRestore();
});
});

describe("compressionAlgorithm", () => {
it("should default to gzip", async () => {
const runTest = SKIP_WEBPACK_4 ? it.skip : it;
runTest("should default to gzip", async () => {
const config = makeWebpackConfig({ analyzerOpts: {} });
await webpackCompile(config, "4.44.2");
await expectValidReport({ parsedSize: 1317, gzipSize: 341 });
});

it("should support gzip", async () => {
runTest("should support gzip", async () => {
const config = makeWebpackConfig({
analyzerOpts: { compressionAlgorithm: "gzip" },
});
await webpackCompile(config, "4.44.2");
await expectValidReport({ parsedSize: 1317, gzipSize: 341 });
});

it("should support brotli", async () => {
runTest("should support brotli", async () => {
const config = makeWebpackConfig({
analyzerOpts: { compressionAlgorithm: "brotli" },
});
Expand All @@ -264,7 +273,7 @@
});

if (isZstdSupported) {
it("should support zstd", async () => {
runTest("should support zstd", async () => {
const config = makeWebpackConfig({
analyzerOpts: { compressionAlgorithm: "zstd" },
});
Expand All @@ -279,4 +288,41 @@
}
});
});
});

Check failure on line 291 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
describe("Issue #499", () => {

Check failure on line 292 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
it("should not cause WebpackLogger 'done hook' error when callback throws", (done) => {

Check failure on line 293 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
expect.assertions(1);

Check failure on line 294 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`

const compiler = webpack({

Check failure on line 296 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Replace `········` with `······`
mode: "development",

Check failure on line 297 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
entry: __filename,

Check failure on line 298 in test/plugin.js

View workflow job for this annotation

GitHub Actions / lint

Replace `··········` with `········`
plugins: [new BundleAnalyzerPlugin({ analyzerMode: "disabled" })],
});

let webpackLoggerError = false;
const originalConsoleError = console.error;

console.error = (...args) => {
const message = args.join(" ");
if (message.includes("No such label 'done hook'")) {
webpackLoggerError = true;
}
originalConsoleError.apply(console, args);
};

compiler.run(() => {
try {
throw new Error("Intentional test error");
} catch {
// Swallow expected error
}
});

setTimeout(() => {
console.error = originalConsoleError;
expect(webpackLoggerError).toBe(false);
done();
}, 1000);
});
});
});
Loading