Skip to content

Commit

Permalink
Fix JS and Wasm/JS test timeouts, bring back Node.js test output (#3953)
Browse files Browse the repository at this point in the history
The module `kotest-tests/kotest-tests-js` exposed failing timeout tests
on the JS platform and missing Node.js test output. This PR fixes those.

`kotest-tests/kotest-tests-js` is still currently disabled as it
requires unpublished versions of the Kotest Gradle and compiler plugins.

#### Testing JS with `kotest-tests/kotest-tests-js`

Preparation

* `settings.gradle.kts`: Added `mavenLocal()` to `repositories`.
* `kotest-tests/kotest-tests-js/build.gradle.kts`: used appropriate
version numbers for `mavenLocal` publication:
  * `id("io.kotest.multiplatform") version "5.9.0-LOCAL"`
  * `kotestCompilerPluginVersion.set("5.9.0-LOCAL")`

Tasks

* `gradlew publishToMavenLocal`
* `gradlew -p kotest-tests/kotest-tests-js --continue cleanAllTests
jsBrowserTest jsNodeTest wasmJsBrowserTest wasmJsNodeTest`
  • Loading branch information
OliverO2 committed Mar 27, 2024
1 parent 1c12b2b commit 2d62856
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ expect fun jasmineTestFrameworkAvailable(): Boolean

// Adapters for test framework functions whose signatures differ between JS and Wasm.

expect fun jasminTestDescribe(name: String, specDefinitions: () -> Unit)

expect fun jasmineTestIt(
description: String,
testFunction: (done: (errorOrNull: Throwable?) -> Unit) -> Any?,
timeout: Int
testFunction: (done: (errorOrNull: Throwable?) -> Unit) -> Any?
)

expect fun jasmineTestXit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.kotest.core.test.TestResult
import io.kotest.engine.PromiseTestCaseExecutionListener
import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory
import io.kotest.engine.interceptors.EngineContext
import io.kotest.engine.jasminTestDescribe
import io.kotest.engine.jasmineTestIt
import io.kotest.engine.test.TestCaseExecutor
import io.kotest.engine.test.interceptors.testNameEscape
Expand All @@ -34,7 +35,7 @@ internal class JasmineTestSpecExecutorDelegate(private val context: EngineContex
override suspend fun execute(spec: Spec): Map<TestCase, TestResult> {
val cc = coroutineContext
// we use the spec itself as an outer/parent test.
describe(testNameEscape(spec::class.bestName())) {
jasminTestDescribe(testNameEscape(spec::class.bestName())) {
materializer.materialize(spec).forEach { root ->

val testDisplayName = testNameEscape(formatter.format(root))
Expand All @@ -59,10 +60,7 @@ internal class JasmineTestSpecExecutorDelegate(private val context: EngineContex
}
// we don't want to return the promise as the js frameworks will use that for test resolution
// instead of the done callback, and we prefer the callback as it allows for custom timeouts
},
// some frameworks default to a 2000 timeout,
// here we set to a high number and use the timeout support kotest provides via coroutines
timeout = Int.MAX_VALUE
}
)
} else {
jasmineTestXit(description = testDisplayName, testFunction = {})
Expand All @@ -72,5 +70,3 @@ internal class JasmineTestSpecExecutorDelegate(private val context: EngineContex
return emptyMap()
}
}

private external fun describe(description: String, specDefinitions: () -> Unit)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package io.kotest.engine
actual fun jasmineTestFrameworkAvailable(): Boolean =
js("typeof describe === 'function' && typeof it === 'function'") as Boolean

actual fun jasminTestDescribe(name: String, specDefinitions: () -> Unit) {
describe(name, specDefinitions)
}

actual fun jasmineTestIt(
description: String,
testFunction: (done: (errorOrNull: Throwable?) -> Unit) -> Any?,
timeout: Int
testFunction: (done: (errorOrNull: Throwable?) -> Unit) -> Any?
) {
it(description, testFunction, timeout)
it(description, testFunction)
}

actual fun jasmineTestXit(
Expand All @@ -20,6 +23,13 @@ actual fun jasmineTestXit(

// Jasmine test framework functions

private external fun it(description: String, testFunction: dynamic, timeout: Int)
@Suppress("UNUSED_PARAMETER")
private fun describe(description: String, specDefinitions: () -> Unit) {
// Here we disable the default 2s timeout and use the timeout support Kotest provides via coroutines.
js("describe(description, function () { this.timeout(0); specDefinitions(); })")
}


private external fun it(description: String, testFunction: dynamic)

private external fun xit(description: String, testFunction: dynamic)
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import kotlin.js.Promise

actual fun jasmineTestFrameworkAvailable(): Boolean = js("typeof describe === 'function' && typeof it === 'function'")

actual fun jasminTestDescribe(name: String, specDefinitions: () -> Unit) {
describe(name, specDefinitions)
}

actual fun jasmineTestIt(
description: String,
testFunction: (done: (errorOrNull: Throwable?) -> Unit) -> Any?,
timeout: Int
testFunction: (done: (errorOrNull: Throwable?) -> Unit) -> Any?
) {
it(description, { done ->
it(description) { done ->
callTestFunction(testFunction) {
callDone(done, it)
}
}, timeout)
}
}

actual fun jasmineTestXit(
Expand Down Expand Up @@ -50,9 +53,11 @@ private fun callDone(
done(errorOrNull?.toJsError())
}

@Suppress("UNUSED_PARAMETER")
private fun jsThrow(jsException: JsAny): Nothing =
js("{ throw jsException; }")

@Suppress("UNUSED_PARAMETER")
private fun throwableToJsError(message: String, stack: String): JsAny =
js("{ const e = new Error(); e.message = message; e.stack = stack; return e; }")

Expand All @@ -61,13 +66,12 @@ private fun Throwable.toJsError(): JsAny =

// Jasmine test framework functions

private external fun it(
description: String,
testFunction: (done: (errorOrNull: JsAny?) -> Unit) -> JsAny?,
timeout: Int
)
@Suppress("UNUSED_PARAMETER")
private fun describe(description: String, specDefinitions: () -> Unit) {
// Here we disable the default 2s timeout and use the timeout support Kotest provides via coroutines.
js("describe(description, function () { this.timeout(0); specDefinitions(); })")
}

private external fun xit(
description: String,
testFunction: (done: (errorOrNull: JsAny?) -> Unit) -> JsAny?
)
private external fun it(description: String, testFunction: (done: (errorOrNull: JsAny?) -> Unit) -> JsAny?)

private external fun xit(description: String, testFunction: (done: (errorOrNull: JsAny?) -> Unit) -> JsAny?)

0 comments on commit 2d62856

Please sign in to comment.