Skip to content

Commit 1bb8c1c

Browse files
committed
Use async tests on Kotlin/JS with Kotlin version 1.2.20-eap-33
1 parent 18d9a6f commit 1bb8c1c

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ buildscript {
1414
repositories {
1515
jcenter()
1616
maven { url "http://kotlin.bintray.com/kotlinx" }
17+
maven { url "http://kotlin.bintray.com/kotlin-eap" }
1718
maven { url "http://kotlin.bintray.com/kotlin-dev" }
1819
maven { url "https://plugins.gradle.org/m2/" }
1920
}
@@ -74,6 +75,7 @@ configure(subprojects.findAll { !sourceless.contains(it.name) }) {
7475
repositories {
7576
jcenter()
7677
maven { url "http://kotlin.bintray.com/kotlinx" }
78+
maven { url "http://kotlin.bintray.com/kotlin-eap" }
7779
maven { url "https://dl.bintray.com/devexperts/Maven/" }
7880
}
7981

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version = 0.21-SNAPSHOT
22

3-
kotlin_version = 1.2.10
3+
kotlin_version = 1.2.20-eap-33
44
junit_version = 4.12
55
atomicFU_version = 0.9.2
66
html_version = 0.6.8

js/example-frontend-js/build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,3 @@ task bundle(type: NpmTask, dependsOn: [npmInstall, runDceKotlinJs]) {
4040
task start(type: NpmTask, dependsOn: bundle) {
4141
args = ["run", "start"]
4242
}
43-
44-
// we have not tests but kotlin-dce-js still tries to work with them and crashed.
45-
// todo: Remove when KT-22028 is fixed
46-
afterEvaluate {
47-
tasks.unpackDependenciesTestKotlinJs.enabled = false
48-
}

js/kotlinx-coroutines-core-js/src/test/kotlin/kotlinx/coroutines/experimental/PromiseTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package kotlinx.coroutines.experimental
1919
import kotlin.js.Promise
2020
import kotlin.test.*
2121

22-
// :todo: This test does not actually test anything because of KT-21970 JS: Support async tests in Mocha and others
23-
// One should watch for errors in the console to see if there were any failures (the test would still pass)
2422
class PromiseTest : TestBase() {
2523
@Test
2624
fun testPromiseResolvedAsDeferred() = promise {
@@ -33,10 +31,13 @@ class PromiseTest : TestBase() {
3331

3432
@Test
3533
fun testPromiseRejectedAsDeferred() = promise {
34+
lateinit var promiseReject: (Throwable) -> Unit
3635
val promise = Promise<String> { _, reject ->
37-
reject(TestException("Rejected"))
36+
promiseReject = reject
3837
}
3938
val deferred = promise.asDeferred()
39+
// reject after converting to deferred to avoid "Unhandled promise rejection" warnings
40+
promiseReject(TestException("Rejected"))
4041
try {
4142
deferred.await()
4243
expectUnreached()

js/kotlinx-coroutines-core-js/src/test/kotlin/kotlinx/coroutines/experimental/TestBase.kt

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package kotlinx.coroutines.experimental
1818

19+
import kotlin.js.*
20+
1921
public actual open class TestBase actual constructor() {
2022
public actual val isStressTest: Boolean = false
2123
public actual val stressTestMultiplier: Int = 1
@@ -66,29 +68,33 @@ public actual open class TestBase actual constructor() {
6668
) {
6769
var exCount = 0
6870
var ex: Throwable? = null
69-
try {
70-
runBlocking(block = block, context = CoroutineExceptionHandler { context, e ->
71-
if (e is CancellationException) return@CoroutineExceptionHandler // are ignored
72-
exCount++
73-
if (exCount > unhandled.size)
74-
error("Too many unhandled exceptions $exCount, expected ${unhandled.size}", e)
75-
if (!unhandled[exCount - 1](e))
76-
error("Unhandled exception was unexpected", e)
77-
context[Job]?.cancel(e)
78-
})
79-
} catch (e: Throwable) {
71+
val promise = promise(block = block, context = CoroutineExceptionHandler { context, e ->
72+
if (e is CancellationException) return@CoroutineExceptionHandler // are ignored
73+
exCount++
74+
if (exCount > unhandled.size)
75+
error("Too many unhandled exceptions $exCount, expected ${unhandled.size}", e)
76+
if (!unhandled[exCount - 1](e))
77+
error("Unhandled exception was unexpected", e)
78+
context[Job]?.cancel(e)
79+
}).catch { e ->
8080
ex = e
8181
if (expected != null) {
8282
if (!expected(e))
8383
error("Unexpected exception", e)
8484
} else
8585
throw e
86-
} finally {
86+
}.finally {
8787
if (ex == null && expected != null) error("Exception was expected but none produced")
88+
if (exCount < unhandled.size)
89+
error("Too few unhandled exceptions $exCount, expected ${unhandled.size}")
90+
error?.let { throw it }
91+
check(actionIndex == 0 || finished) { "Expecting that 'finish(...)' was invoked, but it was not" }
8892
}
89-
if (exCount < unhandled.size)
90-
error("Too few unhandled exceptions $exCount, expected ${unhandled.size}")
91-
error?.let { throw it }
92-
check(actionIndex == 0 || finished) { "Expecting that 'finish(...)' was invoked, but it was not" }
93+
// todo: This is a work-around for missing suspend tests, see KT-22228
94+
@Suppress("UnsafeCastFromDynamic")
95+
return promise.asDynamic()
9396
}
9497
}
98+
99+
private fun <T> Promise<T>.finally(block: () -> Unit): Promise<T> =
100+
then(onFulfilled = { value -> block(); value }, onRejected = { ex -> block(); throw ex })

0 commit comments

Comments
 (0)