Add --only-failures flag to bun:test (#23312)

## Summary

Adds a new `--only-failures` flag to `bun test` that only displays test
failures, similar to `--dots` but without printing dots for each test.

## Motivation

When running large test suites or in CI environments, users often only
care about test failures. The existing `--dots` reporter reduces
verbosity by showing dots, but still requires visual scanning to find
failures. The `--only-failures` flag provides a cleaner output by
completely suppressing passing tests.

## Changes

- Added `--only-failures` CLI flag in `Arguments.zig`
- Added `only_failures` boolean to the test reporters struct in
`cli.zig`
- Updated test output logic in `test_command.zig` to skip non-failures
when flag is set
- Updated `jest.zig` and `bun_test.zig` to handle the new flag
- Added comprehensive tests in `only-failures.test.ts`

## Usage

```bash
bun test --only-failures
```

Example output (only shows failures):
```
test/example.test.ts:
(fail) failing test
error: expect(received).toBe(expected)

Expected: 3
Received: 2

5 pass
1 skip
2 fail
Ran 8 tests across 1 file.
```

## Test Plan

- Verified `--only-failures` flag only shows failing tests
- Verified normal test output still works without the flag
- Verified `--dots` reporter still works correctly
- Added regression tests with snapshot comparisons

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: pfg <pfg@pfg.pw>
This commit is contained in:
robobun
2025-10-19 23:31:29 -07:00
committed by GitHub
parent 74fa49963c
commit 3921f76ff8
9 changed files with 205 additions and 5 deletions

View File

@@ -579,6 +579,7 @@ pub const CommandLineReporter = struct {
reporters: struct {
dots: bool = false,
only_failures: bool = false,
junit: ?*JunitReporter = null,
} = .{},
@@ -874,8 +875,8 @@ pub const CommandLineReporter = struct {
},
}
buntest.reporter.?.last_printed_dot = true;
} else if (Output.isAIAgent() and (comptime result.basicResult()) != .fail) {
// when using AI agents, only print failures
} else if (((comptime result.basicResult()) != .fail) and (buntest.reporter != null and buntest.reporter.?.reporters.only_failures)) {
// when using --only-failures, only print failures
} else {
buntest.bun_test_root.onBeforePrint();
@@ -900,7 +901,7 @@ pub const CommandLineReporter = struct {
var this: *CommandLineReporter = buntest.reporter orelse return; // command line reporter is missing! uh oh!
if (!this.reporters.dots) switch (sequence.result.basicResult()) {
if (!this.reporters.dots and !this.reporters.only_failures) switch (sequence.result.basicResult()) {
.skip => bun.handleOom(this.skips_to_repeat_buf.appendSlice(bun.default_allocator, output_buf.items[initial_length..])),
.todo => bun.handleOom(this.todos_to_repeat_buf.appendSlice(bun.default_allocator, output_buf.items[initial_length..])),
.fail => bun.handleOom(this.failures_to_repeat_buf.appendSlice(bun.default_allocator, output_buf.items[initial_length..])),
@@ -1362,6 +1363,11 @@ pub const TestCommand = struct {
if (ctx.test_options.reporters.dots) {
reporter.reporters.dots = true;
}
if (ctx.test_options.reporters.only_failures) {
reporter.reporters.only_failures = true;
} else if (Output.isAIAgent()) {
reporter.reporters.only_failures = true; // only-failures defaults to true for ai agents
}
js_ast.Expr.Data.Store.create();
js_ast.Stmt.Data.Store.create();