Skip to content

Commit

Permalink
fix: automatically enable coverage when --coverage-reporter is specified
Browse files Browse the repository at this point in the history
Fixes #17502

This change ensures that when a user specifies --coverage-reporter, coverage is automatically enabled, improving the user experience by not requiring an explicit --coverage flag.
  • Loading branch information
Electroid committed Feb 26, 2025
1 parent e4b8b9e commit 44aa307
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,9 @@ pub const Arguments = struct {
}

if (args.options("--coverage-reporter").len > 0) {
// Enable coverage automatically if a coverage reporter is specified
ctx.test_options.coverage.enabled = true;

ctx.test_options.coverage.reporters = .{ .text = false, .lcov = false };
for (args.options("--coverage-reporter")) |reporter| {
if (bun.strings.eqlComptime(reporter, "text")) {
Expand Down
33 changes: 33 additions & 0 deletions test/regression/issue/17502.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { tempDirWithFiles } from "harness";

test("--coverage-reporter automatically enables --coverage", () => {
const dir = tempDirWithFiles("cov-reporter-auto-enables", {
"demo.test.ts": `
export function sum(a, b) {
return a + b;
}
test("sum", () => {
expect(sum(1, 2)).toBe(3);
});
`,
});

// Only specify --coverage-reporter without --coverage
const result = Bun.spawnSync([bunExe(), "test", "--coverage-reporter", "text", "./demo.test.ts"], {
cwd: dir,
env: {
...bunEnv,
},
stdio: [null, null, "pipe"],
});

// If coverage is enabled, we should see coverage output
expect(result.stderr.toString("utf-8")).toContain("File");
expect(result.stderr.toString("utf-8")).toContain("% Funcs");
expect(result.stderr.toString("utf-8")).toContain("% Lines");
expect(result.exitCode).toBe(0);
expect(result.signalCode).toBeUndefined();
});

0 comments on commit 44aa307

Please sign in to comment.