Best Kotest code snippet using io.kotest.engine.test.interceptors.TestNameEscape
createSpecExecutorDelegate.kt
Source:createSpecExecutorDelegate.kt
1package io.kotest.engine.spec2import io.kotest.common.ExperimentalKotest3import io.kotest.core.concurrency.CoroutineDispatcherFactory4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.spec.Spec6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8import io.kotest.engine.PromiseTestCaseExecutionListener9import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory10import io.kotest.engine.describe11import io.kotest.engine.it12import io.kotest.engine.listener.TestEngineListener13import io.kotest.engine.test.TestCaseExecutor14import io.kotest.engine.test.scopes.TerminalTestScope15import io.kotest.engine.test.interceptors.testNameEscape16import io.kotest.engine.test.names.getDisplayNameFormatter17import io.kotest.engine.test.status.isEnabledInternal18import io.kotest.engine.xit19import io.kotest.mpp.bestName20import kotlinx.coroutines.GlobalScope21import kotlinx.coroutines.promise22import kotlin.coroutines.coroutineContext23@ExperimentalKotest24internal actual fun createSpecExecutorDelegate(25 listener: TestEngineListener,26 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,27 configuration: ProjectConfiguration,28): SpecExecutorDelegate = JavascriptSpecExecutorDelegate(configuration)29/**30 * Note: we need to use this: https://youtrack.jetbrains.com/issue/KT-2222831 */32@ExperimentalKotest33internal class JavascriptSpecExecutorDelegate(private val configuration: ProjectConfiguration) : SpecExecutorDelegate {34 private val formatter = getDisplayNameFormatter(35 configuration.registry,36 configuration37 )38 private val materializer = Materializer(configuration)39 override suspend fun execute(spec: Spec): Map<TestCase, TestResult> {40 val cc = coroutineContext41 // we use the spec itself as an outer/parent test.42 describe(testNameEscape(spec::class.bestName())) {43 materializer.materialize(spec).forEach { root ->44 val testDisplayName = testNameEscape(formatter.format(root))45 // todo find a way to delegate this to the test case executor46 val enabled = root.isEnabledInternal(configuration)47 if (enabled.isEnabled) {48 // we have to always invoke `it` to start the test so that the js test framework doesn't exit49 // before we invoke our callback. This also gives us the handle to the done callback.50 val test = it(testDisplayName) { done ->51 // ideally we'd just launch the executor and have the listener setup the test52 // but we can't launch a promise inside the describe and have it resolve the "it"53 // this means we must duplicate the isEnabled check outside of the executor54 GlobalScope.promise {55 TestCaseExecutor(56 PromiseTestCaseExecutionListener(done),57 NoopCoroutineDispatcherFactory,58 configuration59 ).execute(root, TerminalTestScope(root, cc))60 }61 // we don't want to return the promise as the js frameworks will use that for test resolution62 // instead of the done callback, and we prefer the callback as it allows for custom timeouts63 Unit64 }65 // some frameworks default to a 2000 timeout,66 // here we set to a high number and use the timeout support kotest provides via coroutines67 test.timeout(Int.MAX_VALUE)68 Unit69 } else {70 xit(testDisplayName) {}71 }72 }73 }74 return emptyMap()75 }76}...
TestNameEscapeTest.kt
Source:TestNameEscapeTest.kt
1package com.sksamuel.kotest.engine.test2import io.kotest.core.spec.style.FunSpec3import io.kotest.engine.test.interceptors.testNameEscape4import io.kotest.matchers.shouldBe5class TestNameEscapeTest : FunSpec() {6 init {7 test("should escape periods") {8 testNameEscape("foo.bar") shouldBe "foo bar"9 }10 }11}...
TestNameEscape.kt
Source:TestNameEscape.kt
1package io.kotest.engine.test.interceptors2/**3 * Escapes/fixes test names that break on JS.4 * https://www.jetbrains.com/help/teamcity/service-messages.html#Interpreting+test+names5 * https://github.com/kotest/kotest/issues/23026 */7fun testNameEscape(name: String): String {8 return name.replace('.', ' ')9}...
TestNameEscape
Using AI Code Generation
1val listener = object : TestEngineListener {2override fun testStarted(testCase: TestCase) {3println("Test started: ${testCase.description.name}")4}5override fun testFinished(testCase: TestCase, result: TestResult) {6println("Test finished: ${testCase.description.name}")7}8}9val engine = KotestEngine(10interceptors = listOf(TestNameEscapeInterceptor),11engine.execute(spec)12}
TestNameEscape
Using AI Code Generation
1@Deprecated("Use TestNameEscape instead", ReplaceWith("TestNameEscape"))2fun TestEngineLauncher.withTestNameEscape(): TestEngineLauncher3@Deprecated("Use TestNameEscape instead", ReplaceWith("TestNameEscape"))4fun TestEngineLauncher.withTestNameEscape(): TestEngineLauncher5fun TestEngineLauncher.withTestNameEscape(): TestEngineLauncher6@Deprecated("Use TestNameEscape instead", ReplaceWith("TestNameEscape"))7fun TestEngineLauncher.withTestNameEscape(): TestEngineLauncher
TestNameEscape
Using AI Code Generation
1 TestNameEscapeInterceptor()2class TestNameEscapeInterceptor : TestExecutionInterceptor {3 override suspend fun intercept(4 test: suspend (TestCase, TestContext) -> Unit,5 ) {6 val escapedTestName = TestNameEscape.escape(testCase.description.name)7 val escapedTestCase = testCase.copy(description = testCase.description.copy(name = escapedTestName))8 test(escapedTestCase, testContext)9 }10}11class TestNameEscape {12 companion object {13 fun escape(testName: String): String {14 return testName.replace(Regex("[^a-zA-Z0-9]"), "_")15 }16 }17}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!