I’m encountering an UncompletedCoroutinesError while running a Kotlin coroutine test. The specific error message is:
kotlinx.coroutines.test.UncompletedCoroutinesError: After waiting for 1m, the test coroutine is not completing
This error occurs during my test named Test whether the Example widget icons are displayed in zero state is null within the KotlinExampleTests class.
Here’s the relevant code snippet:
@Test
fun `Test whether the Example widget icons are displayed in zero state`() = runTestSafe {
val dispatcher = StandardTestDispatcher(testScheduler)
val viewModel = createViewModel(dispatcher, this)
viewModel.responseFlow.value = mockResponseFlow()
composeTestRule.setContent {
ExampleWidget(
Modifier,
mockZeroState(),
{ _, _-> },
{ _, _ -> },
{ _, _ -> },
{ _ -> },
{ _-> }
)
}
composeTestRule.onNodeWithTag("Tag1").assertExists()
composeTestRule.onNodeWithTag("Tag2").assertExists()
composeTestRule.onNodeWithTag("Tag3").assertExists()
assert(0 == viewModel.mockResponseFlow.value?.icons?.size)
}
What I’ve tried:
I’ve increased the timeout value passed to runTest (currently set to 1 minute).
I’ve verified that all launched coroutines within the test are properly cancelled at the end of the test.
Expected Behavior:
The test should complete successfully within a reasonable timeframe.
Actual Behavior:
The test fails with the UncompletedCoroutinesError indicating that a coroutine launched within the test is not completing even after a 1-minute timeout.
Additional Info
-> The tests are passing when running individual test suite
-> but they are failing when whole test suite
runs
What could be causing this error?
How can I troubleshoot and fix the test to avoid the UncompletedCoroutinesError?
Kotiswar Kaithepalli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.