Documentation
¶
Overview ¶
Package charmtest provides a testing framework for Bubble Tea applications.
It simulates the Bubble Tea runtime without requiring a real terminal, allowing you to send messages, simulate key presses, and assert on the rendered output and model state.
Basic usage:
func TestMyModel(t *testing.T) {
m := NewMyModel()
sim := charmtest.New(m)
sim.SendKey("j") // simulate pressing "j"
sim.SendKey("enter") // simulate pressing enter
charmtest.RequireView(t, sim, "expected output")
}
Index ¶
- Constants
- func DumpMessages(t *testing.T, s *Simulator)
- func DumpView(t *testing.T, s *Simulator)
- func RequireSnapshot(t *testing.T, s *Simulator)
- func RequireSnapshotNamed(t *testing.T, s *Simulator, name string)
- func RequireView(t *testing.T, s *Simulator, expected string)
- func RequireViewContains(t *testing.T, s *Simulator, substr string)
- func RequireViewLines(t *testing.T, s *Simulator, expected int)
- func RequireViewNotContains(t *testing.T, s *Simulator, substr string)
- func ViewString(s *Simulator) string
- type Option
- type Simulator
- func (s *Simulator) Messages() []tea.Msg
- func (s *Simulator) Model() tea.Model
- func (s *Simulator) Resize(width, height int)
- func (s *Simulator) Send(msg tea.Msg)
- func (s *Simulator) SendKey(key string)
- func (s *Simulator) SendKeys(keys ...string)
- func (s *Simulator) Type(text string)
- func (s *Simulator) View() string
- func (s *Simulator) ViewContains(substr string) bool
Constants ¶
const DefaultHeight = 24
DefaultHeight is the default terminal height for simulations.
const DefaultWidth = 80
DefaultWidth is the default terminal width for simulations.
Variables ¶
This section is empty.
Functions ¶
func DumpMessages ¶
DumpMessages prints all messages processed by the simulator to the test log for debugging.
func DumpView ¶
DumpView prints the current view (ANSI-stripped) to the test log for debugging purposes.
func RequireSnapshot ¶
RequireSnapshot compares the current view against a golden file stored in testdata/<testname>.golden. If the golden file does not exist, it is created automatically (the test passes on first run, then validates on subsequent runs).
Set the CHARM_TEST_UPDATE=1 environment variable to overwrite existing golden files when the expected output changes intentionally.
Usage:
func TestMyView(t *testing.T) {
sim := charmtest.New(NewMyModel())
sim.SendKey("j")
charmtest.RequireSnapshot(t, sim)
}
func RequireSnapshotNamed ¶
RequireSnapshotNamed is like RequireSnapshot but uses a custom name for the golden file instead of the test name. Useful when a single test captures multiple snapshots at different points.
func RequireView ¶
RequireView asserts that the simulator's current view matches the expected string exactly. ANSI escape sequences are stripped before comparison.
func RequireViewContains ¶
RequireViewContains asserts that the stripped view contains the given substring.
func RequireViewLines ¶
RequireViewLines asserts that the view has exactly the expected number of non-empty lines.
func RequireViewNotContains ¶
RequireViewNotContains asserts that the stripped view does NOT contain the given substring.
func ViewString ¶
ViewString returns the current view with ANSI sequences stripped, useful for debugging in tests.
Types ¶
type Simulator ¶
type Simulator struct {
// contains filtered or unexported fields
}
Simulator drives a Bubble Tea model through its lifecycle without a real terminal. It processes messages synchronously, making tests deterministic.
func New ¶
New creates a Simulator that wraps the given Bubble Tea model. The model's Init() is called and resulting commands are processed.
func (*Simulator) Model ¶
Model returns the current model state. Use type assertion to access your specific model type:
m := sim.Model().(*MyModel)
func (*Simulator) SendKey ¶
SendKey simulates a key press. Accepts key names like "enter", "esc", "tab", "up", "down", "ctrl+c", or single characters like "a", "j".
func (*Simulator) ViewContains ¶
ViewContains returns true if the current view contains the given substring.