Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(teatest): add WaitForMsg function #310

Open
Broderick-Westrope opened this issue Dec 25, 2024 · 0 comments · May be fixed by #311
Open

feat(teatest): add WaitForMsg function #310

Broderick-Westrope opened this issue Dec 25, 2024 · 0 comments · May be fixed by #311
Labels
enhancement New feature or request

Comments

@Broderick-Westrope
Copy link

Broderick-Westrope commented Dec 25, 2024

Is your feature request related to a problem? Please describe.

I find it challenging to test individual child models in TUIs built using a tree of models, as described in this blog post. This issue arises because the teatest TestModel does not provide a way to access or interact with the underlying tea.Model before the tea.Program is "finished" (usually triggered by returning tea.QuitCmd from the model).

The current API supports inspecting string output mid-run via TestModel.Output, but this is insufficient when testing models that communicate state changes via custom tea.Msgs. This limitation forces testing of individual models through their parent/root models or requires forking teatest to expose this functionality.

Describe the solution you'd like

A TestModel.WaitForMsg function that blocks until a specific tea.Msg is received. This would enable testing of tea.Msg-driven state transitions in models without relying solely on string output. The existing TestModel.WaitFor function, which blocks for specific output, should also be renamed to WaitForOutput for clarity.

This change would facilitate a more robust approach to testing TUIs by allowing:

  1. End-to-end tests: Using WaitForOutput to verify user-facing outputs.
  2. Integration tests: Using WaitForMsg to assert program state transitions and verify key tea.Msg emissions.

Describe alternatives you've considered

  1. Forking the teatest package to expose the inner tea.Model during execution, but this approach is complex since tea.Program does not natively support accessing the model mid-run.
  2. Testing models through their root model, which introduces unnecessary complexity and reduces test isolation.

Ultimately, I shifted focus towards blackbox testing (WaitForOutput) and integration tests (WaitForMsg). This approach aligns with TUI testing being closer to integration testing than unit testing.

Additional context

This feature request stems from testing a TUI structured with nested models in my open-source projects, such as Tetrigo (see this PR). In these cases, child models emit custom tea.Msgs to signal state changes (e.g., transitioning from a menu to the game screen).

I have a working branch implementing WaitForMsg in teatest and an example in my project. I will create a PR to link my current code to this issue, but expect further discussions to take place before making changes to the API.

In addition to the mentioned changes, I have also proposed a rename of the existing function WaitFor to WaitForOutput in the hope that it will not get confused with the new method.

@Broderick-Westrope Broderick-Westrope added the enhancement New feature or request label Dec 25, 2024
@Broderick-Westrope Broderick-Westrope linked a pull request Dec 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant