Overview
Bubble Tea applications are highly testable thanks to their functional architecture. The framework provides options for mocking input/output and controlling the test environment.Test Program Options
Bubble Tea provides several options for creating testable programs:WithInput
Mock keyboard input:options.go:36-45
WithOutput
Capture program output:options.go:28-34
WithoutRenderer
Disable the renderer for simpler testing:options.go:98-102
WithWindowSize
Set initial window dimensions:options.go:159-168
WithContext
Control program lifecycle with context:options.go:19-26
Complete Test Example
Here’s a full test from the Bubble Tea source:tea_test.go:68-88
Testing Update Logic
Test your Update method in isolation:Testing View Output
Test View rendering:Testing Commands
Test command execution:Testing with Mock Data
Table-Driven Tests
Testing with Specific Color Profiles
Test how your app looks with different color support:Testing Message Flow
Testing Init
Disable Input for Tests
Disable input entirely:options.go:36-45
Integration Tests
Test the full program flow:Best Practices
Test Update in Isolation
Test Update in Isolation
Your Update function is pure - test it directly without running the full program:
Mock External Dependencies
Mock External Dependencies
Create interfaces for external services:
Use Context for Timeouts
Use Context for Timeouts
Always use context.WithTimeout in tests to prevent hanging:
tea_test.go:73-74
Test Commands Separately
Test Commands Separately
Commands are just functions - test them directly:
Use Table-Driven Tests
Use Table-Driven Tests
Test multiple scenarios efficiently:
Test Utilities
Helper Functions
Golden Files
Compare output against golden files:Common Pitfalls
Related
- Debugging - Debug failing tests
- Async Operations - Test commands properly
- Input Handling - Mock keyboard input