Reduce test flakiness by warming up molecule tests (#1226)

This commit is contained in:
jonnyandrew 2023-09-06 11:08:21 +01:00 committed by GitHub
parent 11982e60e9
commit 9e5b46200b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
67 changed files with 406 additions and 1 deletions

View file

@ -14,7 +14,7 @@
* limitations under the License.
*/
plugins {
id("io.element.android-library")
id("io.element.android-compose-library")
alias(libs.plugins.ksp)
}
@ -31,4 +31,5 @@ dependencies {
implementation(libs.coroutines.test)
implementation(projects.libraries.core)
implementation(libs.test.turbine)
implementation(libs.molecule.runtime)
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.tests.testutils
import app.cash.molecule.RecompositionMode
import app.cash.molecule.moleculeFlow
import app.cash.turbine.test
import kotlinx.coroutines.test.runTest
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import kotlin.time.Duration.Companion.seconds
/**
* moleculeFlow can take time to initialise during the first test of any given
* test class.
*
* Applying this test rule ensures that the slow initialisation is not done
* inside runTest which has a short default timeout.
*/
class WarmUpRule: TestRule {
override fun apply(base: Statement, description: Description): Statement = object: Statement() {
override fun evaluate() {
runTest(timeout = 60.seconds) {
moleculeFlow(RecompositionMode.Immediate) {
// Do nothing
}.test {
awaitItem() // Await a Unit composition
}
}
base.evaluate()
}
}
}