Skip to content

Commit 3c0db60

Browse files
committed
test: update test coverage
1 parent 300657a commit 3c0db60

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

src/plugin.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ export class FilterChunkWebpackPlugin {
6565
for (const { matcher, label } of ruleMatchers) {
6666
if (await matcher(filename, asset)) {
6767
toKeep.add(filename);
68-
if (!keepLabels.has(filename)) {
69-
keepLabels.set(filename, label);
70-
}
68+
keepLabels.set(filename, label);
7169
break;
7270
}
7371
}
@@ -87,7 +85,8 @@ export class FilterChunkWebpackPlugin {
8785

8886
// Log kept files instead of removed
8987
for (const filename of toKeep) {
90-
const label = keepLabels.get(filename) ?? "[unknown]";
88+
// Label is guaranteed to exist since we set it when adding to toKeep
89+
const label = keepLabels.get(filename)!;
9190
logger.kept(filename, label);
9291
}
9392

tests/unit/logger.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ describe("Logger", () => {
3737
});
3838
});
3939

40+
describe("kept", () => {
41+
it("does not log when debug is false", () => {
42+
const logger = new Logger(false);
43+
logger.kept("main.js", "scripts");
44+
45+
expect(consoleSpy).not.toHaveBeenCalled();
46+
});
47+
48+
it("logs with label when debug is true", () => {
49+
const logger = new Logger(true);
50+
logger.kept("main.js", "scripts");
51+
52+
expect(consoleSpy).toHaveBeenCalledWith("[FilterChunk] Kept: main.js (label: scripts)");
53+
});
54+
});
55+
4056
describe("summary", () => {
4157
it("does not log summary when debug is false and no webpack logger", () => {
4258
const logger = new Logger(false);
@@ -68,6 +84,46 @@ describe("Logger", () => {
6884
expect(consoleSpy).toHaveBeenCalledWith(" licenses:");
6985
expect(consoleSpy).toHaveBeenCalledWith(" - c.LICENSE.txt");
7086
});
87+
88+
it("logs include mode summary when debug is true", () => {
89+
const logger = new Logger(true);
90+
logger.kept("main.js", "scripts");
91+
logger.kept("vendor.js", "scripts");
92+
logger.summary(5, 3, true);
93+
94+
expect(consoleSpy).toHaveBeenCalledWith("[FilterChunk] Kept 2 of 5 assets (3 removed)");
95+
});
96+
97+
it("logs grouped kept files by label in include mode", () => {
98+
const logger = new Logger(true);
99+
logger.kept("main.js", "scripts");
100+
logger.kept("vendor.js", "scripts");
101+
logger.kept("style.css", "styles");
102+
logger.summary(5, 2, true);
103+
104+
expect(consoleSpy).toHaveBeenCalledWith("[FilterChunk] Kept assets:");
105+
expect(consoleSpy).toHaveBeenCalledWith(" scripts:");
106+
expect(consoleSpy).toHaveBeenCalledWith(" - main.js");
107+
expect(consoleSpy).toHaveBeenCalledWith(" - vendor.js");
108+
expect(consoleSpy).toHaveBeenCalledWith(" styles:");
109+
expect(consoleSpy).toHaveBeenCalledWith(" - style.css");
110+
});
111+
112+
it("does not log filtered assets list when none filtered in exclude mode", () => {
113+
const logger = new Logger(true);
114+
logger.summary(5, 0);
115+
116+
expect(consoleSpy).toHaveBeenCalledWith("[FilterChunk] Filtered 0 of 5 assets (5 remaining)");
117+
expect(consoleSpy).not.toHaveBeenCalledWith("[FilterChunk] Filtered assets:");
118+
});
119+
120+
it("does not log kept assets list when none kept in include mode", () => {
121+
const logger = new Logger(true);
122+
logger.summary(5, 5, true);
123+
124+
expect(consoleSpy).toHaveBeenCalledWith("[FilterChunk] Kept 0 of 5 assets (5 removed)");
125+
expect(consoleSpy).not.toHaveBeenCalledWith("[FilterChunk] Kept assets:");
126+
});
71127
});
72128

73129
describe("webpack logger integration", () => {

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default defineConfig({
99
provider: "v8",
1010
reporter: ["text", "lcov"],
1111
include: ["src/**/*.ts"],
12-
exclude: ["src/index.ts"],
12+
exclude: ["src/index.ts", "src/types.ts"],
1313
},
1414
},
1515
});

0 commit comments

Comments
 (0)