Skip to content

Commit 0ebe1e5

Browse files
authored
test: add coverage for generateStatsFile and statsFilename (#735)
Co-authored-by: TopDaily Dev <315340167+topdaily-dev@users.noreply.github.com>
1 parent c6f85b2 commit 0ebe1e5

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

‎test/plugin.js‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,94 @@ describe("Plugin", () => {
290290
});
291291
});
292292

293+
describe("generateStatsFile", () => {
294+
function readStats(statsFilename = "stats.json") {
295+
return JSON.parse(
296+
fs.readFileSync(
297+
path.resolve(__dirname, `./output/${statsFilename}`),
298+
"utf8",
299+
),
300+
);
301+
}
302+
303+
forEachWebpackVersion(({ it, webpackCompile }) => {
304+
it("should generate a stats file", async () => {
305+
const config = makeWebpackConfig({
306+
analyzerOpts: {
307+
analyzerMode: "disabled",
308+
generateStatsFile: true,
309+
},
310+
});
311+
312+
await webpackCompile(config);
313+
314+
expect(readStats().assets.map((asset) => asset.name)).toContain(
315+
"bundle.js",
316+
);
317+
});
318+
});
319+
320+
it("should not generate a stats file by default", async () => {
321+
const config = makeWebpackConfig({
322+
analyzerOpts: {
323+
analyzerMode: "disabled",
324+
},
325+
});
326+
327+
await webpackCompile(config, "4");
328+
329+
expect(
330+
fs.existsSync(path.resolve(__dirname, "./output/stats.json")),
331+
).toBe(false);
332+
});
333+
334+
it("should support a custom `statsFilename`", async () => {
335+
const config = makeWebpackConfig({
336+
analyzerOpts: {
337+
analyzerMode: "disabled",
338+
generateStatsFile: true,
339+
statsFilename: "custom-stats.json",
340+
},
341+
});
342+
343+
await webpackCompile(config, "4");
344+
345+
expect(readStats("custom-stats.json").assets).toBeDefined();
346+
});
347+
348+
it("should create missing directories for a nested `statsFilename`", async () => {
349+
const config = makeWebpackConfig({
350+
analyzerOpts: {
351+
analyzerMode: "disabled",
352+
generateStatsFile: true,
353+
statsFilename: "nested/dir/stats.json",
354+
},
355+
});
356+
357+
await webpackCompile(config, "4");
358+
359+
expect(readStats("nested/dir/stats.json").assets).toBeDefined();
360+
});
361+
362+
it("should support an absolute `statsFilename`", async () => {
363+
const statsFilepath = path.resolve(
364+
__dirname,
365+
"./output/absolute/stats.json",
366+
);
367+
const config = makeWebpackConfig({
368+
analyzerOpts: {
369+
analyzerMode: "disabled",
370+
generateStatsFile: true,
371+
statsFilename: statsFilepath,
372+
},
373+
});
374+
375+
await webpackCompile(config, "4");
376+
377+
expect(fs.existsSync(statsFilepath)).toBe(true);
378+
});
379+
});
380+
293381
describe("reportTitle", () => {
294382
it("should have a sensible default", async () => {
295383
const config = makeWebpackConfig();

0 commit comments

Comments
 (0)