John O'Reilly
12/29/2021, 11:19 AMruntTest in commonTest tests? I have a basic test that tests flow which works if run for Android or JVM but for iOS target I'm getting following.
UncompletedCoroutinesError: After waiting for 60000 ms, the test coroutine is not completingMartin Rajniak
12/29/2021, 5:27 PMrunTest in commonTest for iOS
https://github.com/MartinRajniak/CatViewerDemo/blob/main/shared/src/commonTest/kotlin/eu/rajniak/cat/CatsTest.ktMartin Rajniak
12/29/2021, 5:30 PMrunTest docs
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/run-test.html
that don't mention case when dispatcher is injected in production code (and should be replaced in tests).John O'Reilly
12/29/2021, 5:32 PMMartin Rajniak
12/29/2021, 5:34 PMkotlinx-coroutines-test again after I read that in Coroutines 1.6 they added
•Switched in this commit https://github.com/MartinRajniak/CatViewerDemo/commit/5f6ba59489f306515ed59a719f30e0fbc04bf9ff#diff-7907192102e6f5b21406[…]c38ca72dbd8f450bca381e762with the new reworked API and multiplatform supportkotlinx-coroutines-test
Martin Rajniak
12/29/2021, 5:40 PMJohn O'Reilly
12/29/2021, 9:43 PMMartin Rajniak
12/30/2021, 6:41 PMAccording to my understanding, this exception occurs when you are using a different dispatcherhttps://stackoverflow.com/questions/61224047/unit-testing-coroutines-runblockingtest-this-job-has-not-completed-yet/67897542#67897542 In StarWarsRepository you are using
withContext(Dispatchers.Default)
I would assume you have to replace that with TestScope in tests (see the quote before).
Now thathttps://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/MIGRATION.md#replace-runblocking-with-runtestworks properly with asynchronous completions,runTestis only occasionally useful. As is, most uses ofrunBlockingin tests come from the need to interact with dispatchers that execute on other threads, likerunBlockingor<http://Dispatchers.IO|Dispatchers.IO>.Dispatchers.Default
Injecting dispatcher worked for me. The main point is to make sure you are using the same test dispatcher instance in the block insidehttps://github.com/Kotlin/kotlinx.coroutines/issues/1204#issuecomment-857354670.runBlockingTest { }
Martin Rajniak
12/30/2021, 6:44 PMwithContext is used
https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-test#virtual-time-support-with-other-dispatchersJohn O'Reilly
12/30/2021, 6:46 PMJohn O'Reilly
12/30/2021, 6:49 PMMartin Rajniak
12/30/2021, 6:50 PMJohn O'Reilly
12/30/2021, 6:51 PMMartin Rajniak
12/30/2021, 7:02 PMJohn O'Reilly
12/30/2021, 8:21 PMwithContext code, that isn't coming in to play in this particular test but agreed that should probably inject dispatcher for that