Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/cli/Arguments.zig
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ pub fn parse(allocator: std.mem.Allocator, ctx: Command.Context, comptime cmd: C
}

if (args.options("--coverage-reporter").len > 0) {
// Enable coverage when --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
43 changes: 43 additions & 0 deletions test/cli/test/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,46 @@ test("coverage excludes node_modules directory", () => {
expect(result.exitCode).toBe(0);
expect(result.signalCode).toBeUndefined();
});

test("--coverage-reporter enables coverage", () => {
const dir = tempDirWithFiles("cov", {
"demo.test.ts": `
import { test, expect } from "bun:test";

export function add(a, b) {
return a + b;
}

export function multiply(a, b) {
return a * b;
}

test("add", () => {
expect(add(1, 2)).toBe(3);
});

// Call add but not multiply to have partial coverage
add(5, 10);
`,
});

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

const stderr = result.stderr.toString("utf-8");

// Check that coverage reporting actually happened
expect(stderr).toContain("File");
expect(stderr).toContain("% Funcs");
expect(stderr).toContain("% Lines");
expect(stderr).toContain("demo.test.ts");

expect(result.exitCode).toBe(0);
expect(result.signalCode).toBeUndefined();
});