Best Kotest code snippet using io.kotest.engine.spec.interceptor.IgnoreNestedSpecStylesInterceptor
SpecExecutor.kt
Source:SpecExecutor.kt
...16import io.kotest.engine.spec.interceptor.ApplyExtensionsInterceptor17import io.kotest.engine.spec.interceptor.ConfigurationInContextInterceptor18import io.kotest.engine.spec.interceptor.EnabledIfSpecInterceptor19import io.kotest.engine.spec.interceptor.FinalizeSpecInterceptor20import io.kotest.engine.spec.interceptor.IgnoreNestedSpecStylesInterceptor21import io.kotest.engine.spec.interceptor.IgnoredSpecInterceptor22import io.kotest.engine.spec.interceptor.PrepareSpecInterceptor23import io.kotest.engine.spec.interceptor.ProjectContextInterceptor24import io.kotest.engine.spec.interceptor.RequiresTagSpecInterceptor25import io.kotest.engine.spec.interceptor.SpecExtensionInterceptor26import io.kotest.engine.spec.interceptor.SpecFilterInterceptor27import io.kotest.engine.spec.interceptor.SpecFinishedInterceptor28import io.kotest.engine.spec.interceptor.SpecRefExtensionInterceptor29import io.kotest.engine.spec.interceptor.SpecRefInterceptor30import io.kotest.engine.spec.interceptor.SpecStartedInterceptor31import io.kotest.engine.spec.interceptor.SystemPropertySpecFilterInterceptor32import io.kotest.engine.spec.interceptor.TagsExcludedSpecInterceptor33import io.kotest.mpp.Logger34import io.kotest.mpp.bestName35import kotlin.reflect.KClass36/**37 * Executes a single [SpecRef].38 *39 * Uses a [TestEngineListener] to notify of events in the spec lifecycle.40 *41 * The spec executor has two levels of interceptors:42 * [io.kotest.engine.spec.interceptor.SpecRefInterceptor] are executed before the spec is created.43 * [io.kotest.engine.spec.interceptor.SpecInterceptor] are executed after the spec is created.44 *45 */46@ExperimentalKotest47class SpecExecutor(48 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,49 private val context: EngineContext,50) {51 private val logger = Logger(SpecExecutorDelegate::class)52 private val extensions = SpecExtensions(context.configuration.registry)53 private val listener = context.listener54 suspend fun execute(ref: SpecRef) {55 logger.log { Pair(ref.kclass.bestName(), "Received $ref") }56 referenceInterceptors(ref)57 }58 suspend fun execute(kclass: KClass<out Spec>) {59 execute(SpecRef.Reference(kclass))60 }61 private suspend fun referenceInterceptors(ref: SpecRef) {62 val interceptors = listOfNotNull(63 if (platform == Platform.JVM) EnabledIfSpecInterceptor(listener, context.configuration.registry) else null,64 IgnoredSpecInterceptor(listener, context.configuration.registry),65 SpecFilterInterceptor(listener, context.configuration.registry),66 SystemPropertySpecFilterInterceptor(listener, context.configuration.registry),67 TagsExcludedSpecInterceptor(listener, context.configuration),68 if (platform == Platform.JVM) RequiresTagSpecInterceptor(listener, context.configuration, context.configuration.registry) else null,69 SpecRefExtensionInterceptor(context.configuration.registry),70 SpecStartedInterceptor(listener),71 SpecFinishedInterceptor(listener),72 if (platform == Platform.JVM) ApplyExtensionsInterceptor(context.configuration.registry) else null,73 PrepareSpecInterceptor(context.configuration.registry),74 FinalizeSpecInterceptor(context.configuration.registry),75 )76 val innerExecute: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> = {77 createInstance(ref).flatMap { specInterceptors(it) }78 }79 logger.log { Pair(ref.kclass.bestName(), "Executing ${interceptors.size} reference interceptors") }80 interceptors.foldRight(innerExecute) { ext: SpecRefInterceptor, fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> ->81 { ref -> ext.intercept(ref, fn) }82 }.invoke(ref)83 }84 private suspend fun specInterceptors(spec: Spec): Result<Map<TestCase, TestResult>> {85 val interceptors = listOfNotNull(86 if (platform == Platform.JS) IgnoreNestedSpecStylesInterceptor(listener, context.configuration.registry) else null,87 ProjectContextInterceptor(context.toProjectContext()),88 SpecExtensionInterceptor(context.configuration.registry),89 ConfigurationInContextInterceptor(context.configuration),90 )91 val initial: suspend (Spec) -> Result<Map<TestCase, TestResult>> = {92 try {93 val delegate = createSpecExecutorDelegate(listener, defaultCoroutineDispatcherFactory, context.configuration)94 logger.log { Pair(spec::class.bestName(), "delegate=$delegate") }95 Result.success(delegate.execute(spec))96 } catch (t: Throwable) {97 logger.log { Pair(spec::class.bestName(), "Error executing spec $t") }98 Result.failure(t)99 }100 }...
IgnoreNestedSpecStylesInterceptor.kt
Source:IgnoreNestedSpecStylesInterceptor.kt
...15import io.kotest.mpp.log16/**17 * Filters [Spec]'s that are not compatible on platforms that disallow nested tests.18 */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}
IgnoreNestedSpecStylesInterceptorTest.kt
Source:IgnoreNestedSpecStylesInterceptorTest.kt
...5import io.kotest.core.spec.style.FreeSpec6import io.kotest.core.spec.style.FunSpec7import io.kotest.core.spec.style.WordSpec8import io.kotest.engine.listener.NoopTestEngineListener9import io.kotest.engine.spec.interceptor.IgnoreNestedSpecStylesInterceptor10class IgnoreNestedSpecStylesInterceptorTest : FunSpec({11 test("IgnoreNestedSpecStylesInterceptor should skip any nested spec style") {12 IgnoreNestedSpecStylesInterceptor(NoopTestEngineListener, EmptyExtensionRegistry)13 .intercept(MyBehaviorSpec()) { error("boom") }14 IgnoreNestedSpecStylesInterceptor(NoopTestEngineListener, EmptyExtensionRegistry)15 .intercept(MyWordSpec()) { error("boom") }16 IgnoreNestedSpecStylesInterceptor(NoopTestEngineListener, EmptyExtensionRegistry)17 .intercept(MyFreeSpec()) { error("boom") }18 IgnoreNestedSpecStylesInterceptor(NoopTestEngineListener, EmptyExtensionRegistry)19 .intercept(MyDescribeSpec()) { error("boom") }20 }21})22private class MyBehaviorSpec : BehaviorSpec()23private class MyFreeSpec : FreeSpec()24private class MyWordSpec : WordSpec()25private class MyDescribeSpec : DescribeSpec()...
IgnoreNestedSpecStylesInterceptor
Using AI Code Generation
1class IgnoreNestedSpecStylesInterceptor : SpecExecutionInterceptor {2 override suspend fun intercept(spec: Spec, execute: suspend (Spec) -> Unit, process: suspend (Spec, SpecExecutionInterceptorChain) -> Unit) {3 if (spec is BehaviorSpec || spec is ExpectSpec || spec is FeatureSpec || spec is FreeSpec || spec is FunSpec || spec is StringSpec) {4 execute(spec)5 } else {6 process(spec, this)7 }8 }9}10class NestedSpecStylesTest : StringSpec({11 "should execute nested spec styles" {12 BehaviorSpecTest().execute()13 ExpectSpecTest().execute()14 FeatureSpecTest().execute()15 FreeSpecTest().execute()16 FunSpecTest().execute()17 StringSpecTest().execute()18 }19})20class BehaviorSpecTest : BehaviorSpec({21 given("a given") {22 `when`("a when") {23 then("a then") {24 println("should execute BehaviorSpecTest")25 }26 }27 }28})29class ExpectSpecTest : ExpectSpec({30 context("a context") {31 expect("an expect") {32 println("should execute ExpectSpecTest")33 }34 }35})36class FeatureSpecTest : FeatureSpec({37 feature("a feature") {38 scenario("a scenario") {39 println("should execute FeatureSpecTest")40 }41 }42})43class FreeSpecTest : FreeSpec({44 "a context" - {45 "an expect" {46 println("should execute FreeSpecTest")47 }48 }49})50class FunSpecTest : FunSpec({51 test("a test") {52 println("should execute FunSpecTest")53 }54})55class StringSpecTest : StringSpec({56 "a test" {57 println("should execute StringSpecTest")58 }59})
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!!