Kotlin Test library for experimental WebAssembly JS platform - provides test framework adapters and assertions for testing Kotlin/Wasm applications that target JavaScript environments
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-test-wasm-js@2.2.0Kotlin Test WebAssembly JS provides comprehensive testing functionality for WebAssembly applications targeting JavaScript environments. This library enables developers to write unit tests for Kotlin/Wasm code using familiar kotlin.test APIs with automatic integration to popular JavaScript testing frameworks like Jasmine, Mocha, Jest, and TeamCity.
org.jetbrains.kotlin:kotlin-test-wasm-js:2.2.0import kotlin.test.*For test annotations and all assertion functions:
import kotlin.test.Test
import kotlin.test.BeforeTest
import kotlin.test.AfterTest
import kotlin.test.Ignore
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertNotNull
import kotlin.test.assertContains
import kotlin.test.assertFails
import kotlin.test.failFor async testing with JavaScript Promises:
import kotlin.js.Promiseimport kotlin.test.*
import kotlin.js.Promise
class BasicTests {
@BeforeTest
fun setup() {
// Setup code runs before each test
}
@Test
fun equalityAssertions() {
assertEquals(4, 2 + 2)
assertEquals("hello", "hello")
assertEquals(3.14, 3.14, 0.01) // With tolerance for floats
}
@Test
fun booleanAssertions() {
assertTrue(5 > 3)
assertFalse(listOf<String>().isNotEmpty())
}
@Test
fun nullAssertions() {
val value: String? = "test"
assertNotNull(value) // Returns non-null value
assertEquals("test", value) // value is now smart-cast to String
assertNull(null)
}
@Test
fun collectionAssertions() {
val list = listOf(1, 2, 3)
assertContains(list, 2)
assertContentEquals(listOf(1, 2, 3), list)
}
@Test
fun exceptionAssertions() {
assertFails {
throw RuntimeException("Error")
}
val exception = assertFailsWith<IllegalArgumentException> {
throw IllegalArgumentException("Invalid argument")
}
assertEquals("Invalid argument", exception.message)
}
@Test
fun asyncTestWithPromise(): Promise<*> {
return Promise.resolve(42).then { result ->
assertEquals(42, result)
}
}
@Ignore("Test is disabled")
@Test
fun ignoredTest() {
fail("This test should not run")
}
@AfterTest
fun cleanup() {
// Cleanup code runs after each test
}
}Kotlin Test WASM-JS provides a complete testing solution with three main layers:
@Test, @BeforeTest, @AfterTest, @Ignore for marking test functions and lifecycle methodsAsserter interface providing the foundationAnnotations for marking test functions and controlling test lifecycle, providing the foundation for organizing and running tests.
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Test
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class BeforeTest
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class AfterTest
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Ignore(val value: String = "")Essential assertion functions for verifying test conditions, covering equality, boolean, null, and type checks.
fun <T> assertEquals(expected: T, actual: T, message: String? = null)
fun assertEquals(expected: Double, actual: Double, absoluteTolerance: Double, message: String? = null)
fun assertEquals(expected: Float, actual: Float, absoluteTolerance: Float, message: String? = null)
fun assertTrue(actual: Boolean, message: String? = null)
fun assertFalse(actual: Boolean, message: String? = null)
fun <T : Any> assertNotNull(actual: T?, message: String? = null): T
fun assertNull(actual: Any?, message: String? = null)
fun <T> assertSame(expected: T, actual: T, message: String? = null)
fun <T> assertNotSame(illegal: T, actual: T, message: String? = null)Specialized assertion functions for validating collection contents, array equality, and element containment across all Kotlin collection types.
fun <T> assertContains(iterable: Iterable<T>, element: T, message: String? = null)
fun <T> assertContains(array: Array<T>, element: T, message: String? = null)
fun <T> assertContentEquals(expected: Iterable<T>?, actual: Iterable<T>?, message: String? = null)
fun <T> assertContentEquals(expected: Array<T>?, actual: Array<T>?, message: String? = null)
fun assertContains(charSequence: CharSequence, char: Char, ignoreCase: Boolean = false, message: String? = null)
fun <K, V> assertContains(map: Map<K, V>, key: K, message: String? = null)Functions for testing exception behavior and validating error conditions in test code.
inline fun assertFails(block: () -> Any?): Throwable
inline fun assertFails(message: String?, block: () -> Any?): Throwable
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, block: () -> Any?): T
inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Any?): TThe underlying assertion engine that provides the foundation for all assertion functions, with customizable error handling and reporting.
interface Asserter {
fun fail(message: String?): Nothing
fun fail(message: String?, cause: Throwable?): Nothing
fun assertTrue(lazyMessage: () -> String?, actual: Boolean): Unit
fun assertEquals(message: String?, expected: Any?, actual: Any?): Unit
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?): Unit
fun assertNotNull(message: String?, actual: Any?): Unit
}
val asserter: AsserterAutomatic integration with JavaScript testing frameworks including Jasmine, Mocha, Jest, and TeamCity with seamless Promise support for async tests.
// Tests automatically work in any supported JavaScript testing framework
class MyTests {
@Test
fun syncTest() {
assertEquals(4, 2 + 2)
}
@Test
fun asyncTest(): Promise<*> {
return Promise.resolve(42).then { result ->
assertEquals(42, result)
}
}
}/**
* Test annotation marking a function as a test case
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Test
/**
* Test annotation marking a function to run before each test
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class BeforeTest
/**
* Test annotation marking a function to run after each test
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class AfterTest
/**
* Annotation marking a test or test class as ignored
* @param value optional reason for ignoring the test
*/
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Ignore(val value: String = "")
/**
* Interface providing the foundation for all assertion functions
* Handles test failures and assertion logic
*/
interface Asserter {
/**
* Fails the current test with the given message
*/
fun fail(message: String?): Nothing
/**
* Fails the current test with the given message and cause
*/
fun fail(message: String?, cause: Throwable?): Nothing
/**
* Asserts that the value is true
*/
fun assertTrue(lazyMessage: () -> String?, actual: Boolean): Unit
fun assertTrue(message: String?, actual: Boolean): Unit
/**
* Asserts that expected and actual values are equal
*/
fun assertEquals(message: String?, expected: Any?, actual: Any?): Unit
/**
* Asserts that illegal and actual values are not equal
*/
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?): Unit
/**
* Asserts that expected and actual are the same reference
*/
fun assertSame(message: String?, expected: Any?, actual: Any?): Unit
/**
* Asserts that illegal and actual are not the same reference
*/
fun assertNotSame(message: String?, illegal: Any?, actual: Any?): Unit
/**
* Asserts that the value is null
*/
fun assertNull(message: String?, actual: Any?): Unit
/**
* Asserts that the value is not null
*/
fun assertNotNull(message: String?, actual: Any?): Unit
}
/**
* Interface for contributing custom Asserter implementations
*/
interface AsserterContributor {
/**
* Returns an Asserter instance or null if not contributing
*/
fun contribute(): Asserter?
}