Best Kotest code snippet using io.kotest.engine.spec.SpecExtensions.extensions
IgnoreNestedSpecStylesInterceptor.kt
Source:IgnoreNestedSpecStylesInterceptor.kt
...19internal class IgnoreNestedSpecStylesInterceptor(20 private val listener: TestEngineListener,21 registry: ExtensionRegistry,22) : SpecInterceptor {23 private val extensions = SpecExtensions(registry)24 override suspend fun intercept(25 spec: Spec,26 fn: suspend (Spec) -> Result<Map<TestCase, TestResult>>27 ): Result<Map<TestCase, TestResult>> {28 fun isValid(spec: Spec) = when (spec) {29 is FunSpec, is ExpectSpec, is FeatureSpec, is ShouldSpec, is StringSpec -> true30 else -> false31 }32 return if (isValid(spec)) {33 fn(spec)34 } else {35 log { "IgnoreNestedSpecStylesInterceptor: Marking ${spec::class.bestName()} as inactive due to platform limitations" }36 println("WARN: kotest-js only supports top level tests due to underlying platform limitations. '${spec::class.bestName()}' has been marked as ignored")37 runCatching { listener.specIgnored(spec::class, "Disabled due to platform limitations") }38 .flatMap { extensions.ignored(spec::class, "Disabled due to platform limitations") }39 .map { emptyMap() }40 }41 }42}...
EnabledIfSpecInterceptor.kt
Source:EnabledIfSpecInterceptor.kt
...19internal class EnabledIfSpecInterceptor(20 private val listener: TestEngineListener,21 registry: ExtensionRegistry,22) : SpecRefInterceptor {23 private val extensions = SpecExtensions(registry)24 override suspend fun intercept(25 ref: SpecRef,26 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>27 ): Result<Map<TestCase, TestResult>> {28 val enabled = ref.kclass29 .annotation<EnabledIf>()30 ?.wrapper31 ?.newInstanceNoArgConstructor()32 ?.enabled(ref.kclass) ?: true33 return if (enabled) {34 fn(ref)35 } else {36 runCatching { listener.specIgnored(ref.kclass, "Disabled by @EnabledIf") }37 .flatMap { extensions.ignored(ref.kclass, "Disabled by @EnabledIf") }38 .map { emptyMap() }39 }40 }41}...
SpecFilterInterceptor.kt
Source:SpecFilterInterceptor.kt
...15class SpecFilterInterceptor(16 private val listener: TestEngineListener,17 private val registry: ExtensionRegistry18) : SpecRefInterceptor {19 private val extensions = SpecExtensions(registry)20 private val logger = Logger(SpecFilterInterceptor::class)21 override suspend fun intercept(22 ref: SpecRef,23 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>24 ): Result<Map<TestCase, TestResult>> {25 val excluded = registry.all().filterIsInstance<SpecFilter>().mapNotNull {26 val result = it.filter(ref.kclass)27 if (result is SpecFilterResult.Exclude) result else null28 }.firstOrNull()29 logger.log { Pair(ref.kclass.bestName(), "excludedByFilters == $excluded") }30 return if (excluded == null) {31 fn(ref)32 } else {33 val reason = excluded.reason ?: "Disabled by spec filter"34 listener.specIgnored(ref.kclass, reason)35 extensions.ignored(ref.kclass, reason)36 Result.success(emptyMap())37 }38 }39}...
IgnoredSpecInterceptor.kt
Source:IgnoredSpecInterceptor.kt
...20 private val listener: TestEngineListener,21 registry: ExtensionRegistry,22) : SpecRefInterceptor {23 private val logger = Logger(IgnoredSpecInterceptor::class)24 private val extensions = SpecExtensions(registry)25 override suspend fun intercept(26 ref: SpecRef,27 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>28 ): Result<Map<TestCase, TestResult>> {29 val isIgnored = ref.kclass.hasAnnotation<Ignored>()30 logger.log { Pair(ref.kclass.bestName(), "@Ignored == $isIgnored") }31 return if (isIgnored) {32 runCatching { listener.specIgnored(ref.kclass, "Disabled by @Ignored") }33 .flatMap { extensions.ignored(ref.kclass, "Disabled by @Ignored") }34 .map { emptyMap() }35 } else {36 fn(ref)37 }38 }39}...
TagsExcludedDiscoveryExtension.kt
Source:TagsExcludedDiscoveryExtension.kt
...17class TagsExcludedSpecInterceptor(18 private val listener: TestEngineListener,19 private val conf: ProjectConfiguration,20) : SpecRefInterceptor {21 private val extensions = SpecExtensions(conf.registry)22 override suspend fun intercept(23 ref: SpecRef,24 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>25 ): Result<Map<TestCase, TestResult>> {26 val potentiallyActive = conf.runtimeTags().parse().isPotentiallyActive(ref.kclass)27 return if (potentiallyActive) {28 fn(ref)29 } else {30 runCatching { listener.specIgnored(ref.kclass, null) }31 .flatMap { extensions.ignored(ref.kclass, "Skipped by tags") }32 .map { emptyMap() }33 }34 }35}...
SpecExtensionInterceptor.kt
Source:SpecExtensionInterceptor.kt
1package io.kotest.engine.spec.interceptor2import io.kotest.core.config.ExtensionRegistry3import io.kotest.core.extensions.SpecExtension4import io.kotest.core.spec.Spec5import io.kotest.core.test.TestCase6import io.kotest.core.test.TestResult7import io.kotest.engine.spec.SpecExtensions8/**9 * A [SpecInterceptor] that executes all [SpecExtension]s.10 */11internal class SpecExtensionInterceptor(registry: ExtensionRegistry) : SpecInterceptor {12 val extensions = SpecExtensions(registry)13 override suspend fun intercept(14 spec: Spec,15 fn: suspend (Spec) -> Result<Map<TestCase, TestResult>>16 ): Result<Map<TestCase, TestResult>> {17 return extensions.intercept(spec) { fn(spec) } ?: Result.success(emptyMap())18 }19}...
FinalizeSpecInterceptor.kt
Source:FinalizeSpecInterceptor.kt
...6import io.kotest.engine.spec.SpecExtensions7internal class FinalizeSpecInterceptor(8 registry: ExtensionRegistry,9) : SpecRefInterceptor {10 private val extensions = SpecExtensions(registry)11 override suspend fun intercept(12 ref: SpecRef,13 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>14 ): Result<Map<TestCase, TestResult>> {15 return fn(ref)16 .onSuccess { extensions.finalizeSpec(ref.kclass, it, null) }17 .onFailure { extensions.finalizeSpec(ref.kclass, emptyMap(), it) }18 }19}...
PrepareSpecInterceptor.kt
Source:PrepareSpecInterceptor.kt
...5import io.kotest.core.test.TestCase6import io.kotest.core.test.TestResult7import io.kotest.engine.spec.SpecExtensions8class PrepareSpecInterceptor(registry: ExtensionRegistry) : SpecRefInterceptor {9 private val extensions = SpecExtensions(registry)10 override suspend fun intercept(11 ref: SpecRef,12 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>,13 ): Result<Map<TestCase, TestResult>> {14 return extensions15 .prepareSpec(ref.kclass)16 .flatMap { fn(ref) }17 }18}...
extensions
Using AI Code Generation
1import io.kotest.core.extensions.SpecExtension2import io.kotest.core.spec.style.StringSpec3import io.kotest.engine.spec.SpecExtensions4import io.kotest.matchers.shouldBe5class MySpec : StringSpec({6"test" {7}8})9fun main() {10val spec = MySpec()11val extensions = SpecExtensions()12extensions.add(SpecExtension { println("before spec") })13extensions.add(SpecExtension { println("after spec") })14extensions.beforeSpec(spec)15extensions.afterSpec(spec)16}
extensions
Using AI Code Generation
1 extensions(2 MyExtension()3 ) {4 }5}6class MySpec : FunSpec() {7 init {8 extensions(9 MyExtension()10 test("test 1") {11 }12 test("test 2") {13 }14 }15}16class MyProject : ProjectConfig() {17 override fun extensions() = listOf(MyExtension())18}19class MySpec : FunSpec({20 test("test 1").config(extensions = listOf(MyExtension())) {21 }22})23class MySpec : FunSpec() {24 init {25 test("test 1").config(extensions = listOf(MyExtension())) {26 }27 test("test 2").config(extensions = listOf(MyExtension())) {28 }29 }30}31class MyProject : ProjectConfig() {32 override fun extensions() = listOf(My
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!!