Best for
- Use when writing or reviewing Jetpack Compose UI tests, screenshot tests, previews, semantics assertions, fake image loading, keyboard input, focus assertions, interaction state (hover/pressed/focused), or tests for pla…
chrisbanes/skills/skills/compose-ui-testing-patterns/SKILL.md
Use when writing or reviewing Jetpack Compose UI tests, screenshot tests, previews, semantics assertions, fake image loading, keyboard input, focus assertions, interaction state (hover/pressed/focused), or tests for plain state-driven UI composables.
Decision brief
Use when writing or reviewing Jetpack Compose UI tests, screenshot tests, previews, semantics assertions, fake image loading, keyboard input, focus assertions, interaction state (hover/pressed/focused), or tests for plain state-driven UI composables.
Compatibility matrix
| Platform | Status | Evidence | What to check |
|---|---|---|---|
| Codex | Not declared | No explicit evidence | Portability before use |
| Claude Code | Not declared | No explicit evidence | Portability before use |
| Cursor | Not declared | No explicit evidence | Portability before use |
| Gemini CLI | Not declared | No explicit evidence | Portability before use |
Installation
The source command is displayed only when detected. A safe inspection prompt is always available so your agent can explain every action before execution.
npx skills add https://github.com/chrisbanes/skills --skill "skills/compose-ui-testing-patterns"Inspect the Agent Skill "compose-ui-testing-patterns" from https://github.com/chrisbanes/skills/blob/948acbbd6c444d9aef46ef96fa981ea440e0cf0d/skills/compose-ui-testing-patterns/SKILL.md at commit 948acbbd6c444d9aef46ef96fa981ea440e0cf0d. List every install step, command, network request, credential, file read/write, external action, and rollback step. Explain whether it fits my task. Do not install or execute anything until I approve.
Workflow
1. State the behavior and test concern the task asks you to prove. 2. Inspect the existing test against that concern and choose the smallest sufficient seam from the table below. 3. Keep focused edits within the requested test concern. Do not move test-only helpers into producti…
"This UI test is flaky because images load slowly."
Test the smallest UI contract that proves the behavior. Prefer plain state-driven UI tests with callbacks. Add integration only when lifecycle, navigation, DI, or platform behavior is the thing under test.
Review the “Test target choice” section in the pinned source before continuing.
If the screen has a state holder/UI split, test the plain UI composable:
Permission review
No configured static risk pattern was detected
This is not proof of safety. Runtime behavior, indirect dependencies, and hidden external systems are outside the static scan.
Evidence record
| Signal | Value | Evidence type | Meaning |
|---|---|---|---|
| Quality score | 91/100 | Computed | Documentation, specificity, maintenance, and trust rules |
| Repository stars | 983 | Source | Repository attention, not individual Skill quality |
| Compatibility | 0 platforms | Source | Declared in the catalog source record |
| Usage guide | automated source guide | Editorial | Generated or reviewed according to the visible evidence level |
Pinned source
Test the smallest UI contract that proves the behavior. Prefer plain state-driven UI tests with callbacks. Add integration only when lifecycle, navigation, DI, or platform behavior is the thing under test.
| What you need to prove | Test shape |
|---|---|
| Text, button, loading/error branch, conditional content | Plain UI Compose test |
| Callback wiring from click/input | Plain UI Compose test |
| Focus navigation or keyboard behavior | Compose test with key input |
| Visual layout, clipping, elevation, typography, image composition | Screenshot test |
| State holder updates UI correctly | State holder/unit test plus one wiring smoke test |
| Hover, pressed, focused, dragged interaction state | Plain UI test with MutableInteractionSource |
| Navigation, lifecycle, DI integration | Integration test |
If the screen has a state holder/UI split, test the plain UI composable:
composeTestRule.setContent {
ProfileScreen(
state = ProfileUiState(name = "Ada", canSave = true),
onNameChange = {},
onSaveClick = { saved = true },
onBackClick = {},
)
}
composeTestRule.onNodeWithText("Ada").assertIsDisplayed()
composeTestRule.onNodeWithText("Save").performClick()
assertThat(saved).isTrue()
This avoids constructing ViewModels, components, repositories, navigation, and dependency graphs for layout behavior.
Assert semantics when behavior is semantic:
onNodeWithText.assertIsEnabled, assertIsNotEnabled.assertDoesNotExist.Use test tags for nodes that have no stable user-visible text or where multiple nodes share text. Do not use tags as the first choice for all assertions; user-visible semantics are usually stronger.
Use simple counters or captured values:
var selectedId: String? = null
composeTestRule.setContent {
ItemList(
items = listOf(ItemUi("movie-1", "Movie")),
onItemClick = { selectedId = it },
)
}
composeTestRule.onNodeWithText("Movie").performClick()
assertThat(selectedId).isEqualTo("movie-1")
For plain captured callback values, a direct assertion after the action is usually enough. Use runOnIdle when the assertion needs Compose to finish applying snapshot state, recomposition, or queued UI work before reading the result.
For layout, branch, and callback behavior, render controlled state with setContent instead of constructing the production app graph. Production DI, repositories, lifecycle observers, and background effects add asynchronous work that is irrelevant to a plain UI contract and can make the test flaky.
Do not use Thread.sleep to wait for Compose. Drive the UI to a known state, then use semantic assertions and Compose synchronization (waitForIdle, runOnIdle, or a bounded waitUntil for a real asynchronous condition). Reserve full-app integration for behavior that actually depends on navigation, lifecycle, DI, or platform wiring.
When a composable's appearance or behavior depends on interaction state (hover, focus, press, drag), inject a MutableInteractionSource and emit the desired state directly. Do not try to simulate pointer/mouse events to trigger interaction states — that approach is fragile, environment-dependent, and produces flaky tests.
val interactionSource = MutableInteractionSource()
composeTestRule.setContent {
OutlinedButton(
onClick = {},
interactionSource = interactionSource,
)
}
// Assert default (un-hovered) state
composeTestRule.onNodeWithText("OutlinedButton").assertIsDisplayed()
// Emit hover — interactionSource.emit is a suspend function,
// so call it from a test coroutine scope.
TestScope().launch {
interactionSource.emit(HoverInteraction.Enter())
}
composeTestRule.waitForIdle()
// Assert the visual/semantic change that hover produces
// (e.g., border color, elevation, or capture for screenshot test)
composeTestRule.onNodeWithText("OutlinedButton").assertIsDisplayed()
The same pattern works for PressInteraction.Press / Release / Cancel, FocusInteraction.Focus / Unfocus, and DragInteraction.Start / Stop / Cancel. Emit the entry interaction, waitForIdle, then assert the result.
Key points:
MutableInteractionSource rather than relying on the default internal source. This gives you full control over state transitions.TestScope().launch { }) since emit is a suspend function. Do not use LaunchedEffect — that is a production Compose effect, not a test tool.For keyboard, TV, and desktop UI, drive navigation with the same input model users use (keys/D-pad), not clicks alone. Assert focused semantics, not colors or scale; reserve screenshots for visual focus treatment.
Details—focus graph, FocusRequester, restoration, key handlers, and test patterns: compose-focus-navigation.
Use screenshots for visual contracts that semantics cannot prove:
Keep screenshot state deterministic:
When image content is irrelevant, fake the loader and assert the requested model if that is the behavior. The exact hook depends on your image library; a project helper might look like this:
val requestedModels = mutableListOf<Any?>()
// Example helper, not a Compose API.
setContentWithFakeImageLoader { request ->
requestedModels += request.data
errorPainter()
}
When image appearance matters, provide a deterministic local painter/bitmap instead of network data.
| Mistake | Fix |
|---|---|
| Constructing full app graph to test an error row | Test plain UI with state = Error |
| Testing click behavior through a ViewModel mock | Pass a callback and assert it was invoked |
| Screenshot test for simple text presence | Use semantics assertion |
| Semantics test for padding/color/focus ring | Use screenshot test |
| Test tags everywhere | Prefer text/content description/role when stable |
| UI test depends on real image loading/network/time | Fake or freeze the source |
| Sleeping after an action before asserting UI | Use semantics plus waitForIdle, runOnIdle, or bounded waitUntil |
| Production DI or app wiring for a state/rendering assertion | Render controlled state with setContent; use integration only when that wiring is under test |
| Simulating hover/press/focus with mouse or touch events | Inject MutableInteractionSource and emit the interaction |
Relying on the default InteractionSource in tests | Pass MutableInteractionSource so you can control state |
TV/keyboard UI tested with performClick only | Use key input and focus assertions; see compose-focus-navigation |
performMouseInput or touch injection to trigger hover/press states instead of MutableInteractionSource.emit.interactionSource but tests don't inject MutableInteractionSource.Thread.sleep before asserting.Thread.sleep, and only asserts that a node exists. GREEN renders fixed state with setContent, drives the action, synchronizes through Compose, and asserts the semantic state or callback result.NavController lifecycle. GREEN uses an integration test rather than pretending a plain rendering test proves that contract.Frequently asked questions
Use when writing or reviewing Jetpack Compose UI tests, screenshot tests, previews, semantics assertions, fake image loading, keyboard input, focus assertions, interaction state (hover/pressed/focused), or tests for plain state-driven UI composables.
The source record exposes this install command: npx skills add https://github.com/chrisbanes/skills --skill "skills/compose-ui-testing-patterns". Inspect the command and pinned source before running it.
Alternatives
coreyhaines31/marketingskills
When the user wants to plan, design, or implement an A/B test or experiment, or build a growth experimentation program. Also use when the user mentions "A/B test," "split test," "experiment," "test this change," "variant copy," "multivariate test," "hypothesis," "should I test this," "which version is better," "test two versions," "statistical significance," "how long should I run this test," "growth experiments," "experiment velocity," "experiment backlog," "ICE score," "experimentation program
narrative-io/narrative-skills-marketplace
Translate a fuzzy analytical question into a rigorous investigation plan. Interrogates the ask, grounds the plan in the available data dictionary, applies analytical best practices, and produces a structured brief of query specifications for a downstream query-writing skill. Plans, does not write SQL. Use when: "why did X drop", "is there a relationship between A and B", "who are our highest-value customers", "what's driving the change in Y", "investigate this trend", "design an analysis for", "
vasilyu1983/AI-Agents-public
Guides iOS testing with XCTest, XCUITest, Swift Testing, simctl, and xcresult. Use when choosing destinations, controlling flakes, or parsing test artifacts for native apps.
vasilyu1983/AI-Agents-public
Consumer-neuroscience primitives for attention, arousal, bonding, narrative, memory, and reward. Use when shaping ethical UX, neuro study design, or DMCC/AI Act gates.