I wrote konsist test for checking how feature fold...
# konsist
b
I wrote konsist test for checking how feature folders(modules) depend on each other in the project. Each feature folder should depend only on the core feature and not on other features. I am curious if any of you guys had a need to write such a test and how you did it. Or if it makes sense at all to have such a test in place. Check the code in the thread 🧵
Copy code
private val core = Layer("Core", "sp.bvantur.core..")

private val features = listOf(
    Layer("Splash", "sp.bvantur.feature1.."),
    Layer("Register", "sp.bvantur.feature2.."),
    Layer("Login", "sp.bvantur.feature3..")
)

@Test
fun `single features depends only on core feature`() {
    Konsist
        .scopeFromProduction("composeApp")
        .assertArchitecture {
            features.forEach { feature ->
                feature.dependsOn(core)
                features.filter { it != feature }.forEach {
                    feature.doesNotDependOn(it)
                }
            }
        }
}
i
I think your test looks good. (Perhaps some comments would be nice as you are testing two things here) :
Copy code
@Test
fun `single features depends only on core feature`() {
    Konsist
        .scopeFromProduction("composeApp")
        .assertArchitecture {
            features.forEach { feature ->
                // feature depends only on core feature
                feature.dependsOn(core)

                // features does not depend on other feature
                features.filter { it != feature }.forEach {
                    feature.doesNotDependOn(it)
                }
            }
        }
}
Just keep in mind that Konsist describe layer dependencies (whatever given file is using imports from other layer). not module dependencies (sometimes these are aligned like in your case) If
core
layer is independent (from any other layers added inside
assertArchitecture
) then you could also use
dependsOnNothing
):
Copy code
private val core = Layer("Core", "sp.bvantur.core..")

private val features = listOf(
    Layer("Splash", "sp.bvantur.feature1.."),
    Layer("Register", "sp.bvantur.feature2.."),
    Layer("Login", "sp.bvantur.feature3..")
)

@Test
fun `single features depends only on core feature`() {
    Konsist
        .scopeFromProduction("composeApp")
        .assertArchitecture {
            // ...
             core.doesOnNothing()
        }
}
BTW We are currently refactoring
assertArchitecture
(API will not change, but will be extended) . If you see anything off this is a very good time to let us know. BTW Looking at your code I see that there is a potencial to remove
forEach
- add a method working on collections:
Copy code
fun `single features depends only on core feature`() {
    Konsist
        .scopeFromProduction("composeApp")
        .assertArchitecture {
            features.dependsOn(core) // Potential for API improvement on List<Layer>.dependsOn() 
            core.doesOnNothing()
        }
}
b
Thank you @igor.wojda Really useful response. I will apply your suggestions. 🙌