diff --git a/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/LaunchWorkflow.kt b/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/LaunchWorkflow.kt index 359e1eb15..a034edae2 100644 --- a/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/LaunchWorkflow.kt +++ b/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/LaunchWorkflow.kt @@ -28,6 +28,7 @@ import com.squareup.workflow.testing.WorkflowTestParams.StartMode.StartFromWorkf import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import org.jetbrains.annotations.TestOnly +import kotlin.coroutines.CoroutineContext /** * Launches the [workflow] in a new coroutine in [scope]. The workflow tree is seeded with @@ -42,6 +43,7 @@ fun launchWorkflowForTestFr workflow: StatefulWorkflow, props: Flow, testParams: WorkflowTestParams, + workerContext: CoroutineContext, beforeStart: CoroutineScope.(session: WorkflowSession) -> RunnerT ): RunnerT { val initialState = (testParams.startFrom as? StartFromState)?.state @@ -63,6 +65,6 @@ fun launchWorkflowForTestFr initialState = initialState, initialSnapshot = initialSnapshot, beforeStart = beforeStart, - workerContext = testParams.workerContext + workerContext = workerContext ) } diff --git a/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/WorkflowTestParams.kt b/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/WorkflowTestParams.kt index c4f5b9b2c..d789287bf 100644 --- a/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/WorkflowTestParams.kt +++ b/kotlin/workflow-runtime/src/main/java/com/squareup/workflow/testing/WorkflowTestParams.kt @@ -37,8 +37,9 @@ import kotlin.coroutines.EmptyCoroutineContext * It is recommended to leave this on, but if you need to debug a test and don't want to have to * deal with the extra passes, you can temporarily set it to false. * @param workerContext Used to customize the context in which workers are started for tests. - * Default is [EmptyCoroutineContext], which means the workers will use the context from their - * workflow and use the [Unconfined][kotlinx.coroutines.Dispatchers.Unconfined] dispatcher. + * Default is [EmptyCoroutineContext], which means the workers will use their workflow's context + * plus the base context passed into the test method. Any context element present in [workerContext] + * will override all other context elements for workers. */ @TestOnly data class WorkflowTestParams( diff --git a/kotlin/workflow-testing/src/main/java/com/squareup/workflow/testing/WorkflowTester.kt b/kotlin/workflow-testing/src/main/java/com/squareup/workflow/testing/WorkflowTester.kt index 7afc74711..168704125 100644 --- a/kotlin/workflow-testing/src/main/java/com/squareup/workflow/testing/WorkflowTester.kt +++ b/kotlin/workflow-testing/src/main/java/com/squareup/workflow/testing/WorkflowTester.kt @@ -283,7 +283,10 @@ fun scope = CoroutineScope(Unconfined + context + uncaughtExceptionHandler), workflow = this@test, props = propsChannel.asFlow(), - testParams = testParams + testParams = testParams, + // Use the base context as the starting point for the worker context since it's often used + // to pass in test dispatchers, and that's reasonable behavior. + workerContext = context + testParams.workerContext ) { session -> WorkflowTester(this, propsChannel, session.renderingsAndSnapshots, session.outputs) .apply { collectFromWorkflow() }