testing-flows-with-turbine
Installation
SKILL.md
Testing Flows With Turbine — Assert Emissions Without toList Hangs
Turbine is the canonical way to assert Flow emissions one-at-a-time, with built-in cancellation that prevents hot flows (StateFlow, SharedFlow) from hanging the test. This skill covers flow.test, the ReceiveTurbine API, turbineScope for multi-flow tests, and when raw flow.toList() actually beats Turbine. The non-Flow coroutine plumbing is ../testing-coroutines-with-runtest/SKILL.md.
When to use this skill
- The class under test exposes a
StateFlow<UiState>orSharedFlow<Event>and the test must assert each emission. - A test hangs on
flow.toList()over aSharedFloworStateFlow— neither completes. - The developer wants to assert "no more emissions for a beat" or "the next event is an error", which
toList()cannot express. - Two flows must be observed concurrently (e.g.
uiStateplus a one-shoteventsflow) and asserted in interleaved order. - The build error is
TurbineAssertionError: Unconsumed events foundorExpected an item but found Complete/Error. - The developer mentions Turbine APIs:
awaitItem,awaitComplete,awaitError,expectMostRecentItem,skipItems,expectNoEvents,withTurbineTimeout,turbineScope,testIn.
When NOT to use this skill
- The test is about
runTest,TestScope,Dispatchers.setMain, or virtual time. Use../testing-coroutines-with-runtest/SKILL.mdfirst; Turbine sits on top ofrunTest. - The flow is cold and completes deterministically, and the assertion is "the full list equals X".
flow.toList()is shorter — see Decision matrix below. - The "flow" is actually Compose snapshot state. Read it via the test rule, not Turbine.