Kotlin Test library for experimental WebAssembly JS platform - provides test framework adapters and assertions for testing Kotlin/Wasm applications that target JavaScript environments
—
The underlying assertion engine that provides the foundation for all assertion functions. The Asserter system enables customizable error handling, test failure reporting, and extensible assertion logic for Kotlin/WASM-JS testing environments.
The core interface that defines how assertions are processed and how test failures are handled.
/**
* Provides the foundation for all assertion functions
* Handles test failure reporting and assertion logic
*/
interface Asserter {
/**
* Fails the current test with the given message
* @param message the failure message
* @throws AssertionError always throws to indicate test failure
*/
fun fail(message: String?): Nothing
/**
* Fails the current test with the given message and underlying cause
* @param message the failure message
* @param cause the underlying cause of the failure
* @throws AssertionError always throws to indicate test failure
*/
fun fail(message: String?, cause: Throwable?): Nothing
/**
* Asserts that the value is true
* @param lazyMessage function that produces failure message if assertion fails
* @param actual the value to check
*/
fun assertTrue(lazyMessage: () -> String?, actual: Boolean): Unit
/**
* Asserts that the value is true with a simple message
* @param message the failure message
* @param actual the value to check
*/
fun assertTrue(message: String?, actual: Boolean): Unit
/**
* Asserts that expected and actual values are equal
* @param message the failure message
* @param expected the expected value
* @param actual the actual value
*/
fun assertEquals(message: String?, expected: Any?, actual: Any?): Unit
/**
* Asserts that illegal and actual values are not equal
* @param message the failure message
* @param illegal the value that actual should not equal
* @param actual the actual value
*/
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?): Unit
/**
* Asserts that expected and actual are the same object reference
* @param message the failure message
* @param expected the expected object reference
* @param actual the actual object reference
*/
fun assertSame(message: String?, expected: Any?, actual: Any?): Unit
/**
* Asserts that illegal and actual are not the same object reference
* @param message the failure message
* @param illegal the reference that actual should not be
* @param actual the actual object reference
*/
fun assertNotSame(message: String?, illegal: Any?, actual: Any?): Unit
/**
* Asserts that the value is null
* @param message the failure message
* @param actual the value to check
*/
fun assertNull(message: String?, actual: Any?): Unit
/**
* Asserts that the value is not null
* @param message the failure message
* @param actual the value to check
*/
fun assertNotNull(message: String?, actual: Any?): Unit
}Global property providing access to the currently active asserter instance.
/**
* The current asserter instance used by all assertion functions
* This property provides access to the active asserter for custom assertion logic
*/
val asserter: AsserterUsage Example:
import kotlin.test.*
class CustomAssertionTests {
@Test
fun customAssertion() {
// Use the global asserter directly for custom logic
val user = createUser()
if (user.email.isEmpty() || !user.email.contains("@")) {
asserter.fail("User must have a valid email address")
}
// All standard assertions use this same asserter internally
assertTrue(user.isActive)
assertEquals("John Doe", user.name)
}
}Interface for providing custom asserter implementations that can be contributed to the assertion system.
/**
* Interface for contributing custom Asserter implementations
* Allows frameworks and libraries to provide specialized assertion behavior
*/
interface AsserterContributor {
/**
* Returns an Asserter instance or null if not contributing
* @return Asserter instance to use, or null to skip this contributor
*/
fun contribute(): Asserter?
}Usage Example:
import kotlin.test.*
class CustomAsserterContributor : AsserterContributor {
override fun contribute(): Asserter? {
return if (isTestEnvironment()) {
CustomTestAsserter()
} else {
null // Use default asserter
}
}
}
class CustomTestAsserter : Asserter {
override fun fail(message: String?): Nothing {
// Custom failure handling - e.g., enhanced logging
logTestFailure(message)
throw AssertionError(message)
}
override fun fail(message: String?, cause: Throwable?): Nothing {
logTestFailure(message, cause)
throw AssertionError(message, cause)
}
override fun assertTrue(lazyMessage: () -> String?, actual: Boolean) {
if (!actual) {
fail(lazyMessage())
}
}
override fun assertTrue(message: String?, actual: Boolean) {
if (!actual) {
fail(message ?: "Expected true but was false")
}
}
override fun assertEquals(message: String?, expected: Any?, actual: Any?) {
if (expected != actual) {
fail(message ?: "Expected <$expected> but was <$actual>")
}
}
// ... implement other assertion methods
}WebAssembly-specific interfaces and functionality for optimized assertion processing.
/**
* WASM-specific assertion result interface for enhanced assertion processing
* Provides structured information about assertion outcomes
*/
interface AssertionResult {
/**
* Whether the assertion passed or failed
*/
val result: Boolean
/**
* The expected value in the assertion
*/
val expected: Any?
/**
* The actual value in the assertion
*/
val actual: Any?
/**
* Lazy evaluation of the failure message
*/
val lazyMessage: () -> String?
}How high-level assertion functions integrate with the Asserter system:
Example Internal Implementation:
// This shows how assertion functions use the asserter system internally
fun <T> assertEquals(expected: T, actual: T, message: String?) {
asserter.assertEquals(message, expected, actual)
}
fun assertTrue(actual: Boolean, message: String?) {
asserter.assertTrue(message, actual)
}
inline fun assertTrue(message: String?, block: () -> Boolean) {
asserter.assertTrue(message, block())
}
fun fail(message: String?): Nothing {
asserter.fail(message)
}The Asserter system handles consistent error message formatting across all assertion types:
Standard Message Patterns:
"Expected <expected> but was <actual>""Expected true but was false""Expected null but was <actual>"Custom Message Examples:
import kotlin.test.*
class MessageFormattingTests {
@Test
fun customMessages() {
val user = User("John", 25)
assertEquals("User name should match", "Jane", user.name)
// Failure: "User name should match"
assertTrue("User should be adult") { user.age >= 18 }
// Success: no message needed
assertNotNull(user.email, "User must have email configured")
// Failure: "User must have email configured"
}
}The Asserter system seamlessly integrates with JavaScript testing frameworks through automatic adapter detection:
Environment Detection:
// The system automatically detects the testing environment and configures
// the appropriate asserter for optimal integration:
// In Jasmine/Mocha/Jest environments:
// - Assertion failures become JavaScript Error objects
// - Stack traces are preserved and properly formatted
// - Async test failures are handled through Promise rejection
// In TeamCity environments:
// - Failures are reported as ##teamcity[testFailed] messages
// - Test lifecycle is tracked with proper flow IDs
// - Error details are escaped for TeamCity parsingThis system provides a flexible, extensible foundation for all testing assertions while maintaining compatibility with various JavaScript testing environments and frameworks.
Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-test-wasm-js