title: How can I collect values from a mutableSharedFlow in a unit test?
body: <p>The following test doesn't pass. The string &quot;before adding XXX&quot; is never printed.</p>
  <pre class="lang-kotlin prettyprint-override"><code>    @Test
      fun testFoo() = runBlocking {
          val testCoroutineScope = TestCoroutineScope().apply {
              pauseDispatcher() // This needs to be here because the actual test handles time.
          }
          val sharedFlow = MutableSharedFlow&lt;Int&gt;()
          val values = mutableListOf&lt;Int&gt;()
          println(&quot;before launch&quot;)
          val job = testCoroutineScope.launch {
              println(&quot;before collect&quot;)
              sharedFlow.collect {
                  println(&quot;before adding $it&quot;)
                  values.add(it)
              }
          }
          println(&quot;before emits&quot;)
          sharedFlow.emit(1)
          sharedFlow.emit(2)
          testCoroutineScope.runCurrent()
          assertEquals(mutableListOf(1, 2), values)
          job.cancel()
      }
  </code></pre>
  <p>I would like to have a way to <code>emit</code> values once the collect part is handled. I can't set a <code>replay</code> value, nor can I use an <code>onSubscription</code> because that would change the business logic.</p>
  <p>Adding <code>delay</code> or <code>yield</code> don't seem to make any difference so it might not be a racing condition.</p>
  <p>The test passes if <code>pauseDispatcher()</code> is commented.</p>
  <p>My understanding was that <code>emit</code> would suspend until the collect lambda were called.</p>