From f199df2125b8d7b43c0018cb2bb9a910699a5b73 Mon Sep 17 00:00:00 2001 From: TopDaily Dev <315340167+topdaily-dev@users.noreply.github.com> Date: Mon, 24 Aug 2026 19:45:14 +0200 Subject: [PATCH] test: add coverage for generateStatsFile and statsFilename --- test/plugin.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/test/plugin.js b/test/plugin.js index 1a7547ef..6979356d 100644 --- a/test/plugin.js +++ b/test/plugin.js @@ -206,6 +206,94 @@ describe("Plugin", () => { }); }); + describe("generateStatsFile", () => { + function readStats(statsFilename = "stats.json") { + return JSON.parse( + fs.readFileSync( + path.resolve(__dirname, `./output/${statsFilename}`), + "utf8", + ), + ); + } + + forEachWebpackVersion(({ it, webpackCompile }) => { + it("should generate a stats file", async () => { + const config = makeWebpackConfig({ + analyzerOpts: { + analyzerMode: "disabled", + generateStatsFile: true, + }, + }); + + await webpackCompile(config); + + expect(readStats().assets.map((asset) => asset.name)).toContain( + "bundle.js", + ); + }); + }); + + it("should not generate a stats file by default", async () => { + const config = makeWebpackConfig({ + analyzerOpts: { + analyzerMode: "disabled", + }, + }); + + await webpackCompile(config, "4"); + + expect( + fs.existsSync(path.resolve(__dirname, "./output/stats.json")), + ).toBe(false); + }); + + it("should support a custom `statsFilename`", async () => { + const config = makeWebpackConfig({ + analyzerOpts: { + analyzerMode: "disabled", + generateStatsFile: true, + statsFilename: "custom-stats.json", + }, + }); + + await webpackCompile(config, "4"); + + expect(readStats("custom-stats.json").assets).toBeDefined(); + }); + + it("should create missing directories for a nested `statsFilename`", async () => { + const config = makeWebpackConfig({ + analyzerOpts: { + analyzerMode: "disabled", + generateStatsFile: true, + statsFilename: "nested/dir/stats.json", + }, + }); + + await webpackCompile(config, "4"); + + expect(readStats("nested/dir/stats.json").assets).toBeDefined(); + }); + + it("should support an absolute `statsFilename`", async () => { + const statsFilepath = path.resolve( + __dirname, + "./output/absolute/stats.json", + ); + const config = makeWebpackConfig({ + analyzerOpts: { + analyzerMode: "disabled", + generateStatsFile: true, + statsFilename: statsFilepath, + }, + }); + + await webpackCompile(config, "4"); + + expect(fs.existsSync(statsFilepath)).toBe(true); + }); + }); + describe("reportTitle", () => { it("should have a sensible default", async () => { const config = makeWebpackConfig();