or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-assertions.mdcore-assertions.mdexception-handling.mdhamcrest-matchers.mdindex.mdtest-assumptions.mdtest-lifecycle.mdtest-runners.md
tile.json

tessl/maven-org-scala-js--scalajs-junit-test-runtime

JUnit test runtime for Scala.js - provides the runtime infrastructure for running JUnit tests compiled with Scala.js

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.scala-js/scalajs-junit-test-runtime_2.13@1.19.x

To install, run

npx @tessl/cli install tessl/maven-org-scala-js--scalajs-junit-test-runtime@1.19.0

index.mddocs/

Scala.js JUnit Test Runtime

Scala.js JUnit Test Runtime provides the complete JUnit 4 testing framework implementation for Scala.js applications. It enables developers to write and run JUnit-based tests that are compiled with Scala.js and executed in JavaScript environments, bridging the gap between Java's JUnit testing patterns and JavaScript runtime execution.

Package Information

  • Package Name: scalajs-junit-test-runtime
  • Package Type: maven
  • Language: Scala
  • Installation: libraryDependencies += "org.scala-js" %%% "scalajs-junit-test-runtime" % "1.19.0"

Core Imports

import org.junit._
import org.junit.Assert._
import org.junit.Assume._
import org.hamcrest.CoreMatchers._
import org.hamcrest.MatcherAssert._
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.scalajs.junit.JUnitFramework

Basic Usage

import org.junit._
import org.junit.Assert._

class MyTest {
  @Test
  def testBasicAssertion(): Unit = {
    val result = 2 + 2
    assertEquals(4, result)
    assertTrue(result > 0)
  }
  
  @Test
  def testWithHamcrest(): Unit = {
    import org.hamcrest.CoreMatchers._
    val name = "Alice"
    assertThat(name, is(notNullValue()))
    assertThat(name.length, is(5))
  }
  
  @Before
  def setUp(): Unit = {
    // Setup code before each test
  }
  
  @After  
  def tearDown(): Unit = {
    // Cleanup code after each test
  }
}

Architecture

The Scala.js JUnit Test Runtime is organized around several key components:

  • JUnit Core API: Complete implementation of JUnit 4 assertion and assumption methods
  • Lifecycle Management: Full support for @Test, @Before, @After, @BeforeClass, @AfterClass annotations
  • Hamcrest Integration: Core matcher library for expressive test assertions
  • Scala.js Runtime: SBT testing framework integration for JavaScript execution
  • Exception Handling: Enhanced error reporting with stack trace filtering and comparison diffs
  • Test Discovery: Automatic test detection and execution via SBT testing framework

Capabilities

Core Assertions

Fundamental assertion methods for test validation including equality, null checks, boolean conditions, and exception testing.

object Assert {
  def assertTrue(condition: Boolean): Unit
  def assertTrue(message: String, condition: Boolean): Unit
  def assertFalse(condition: Boolean): Unit
  def assertFalse(message: String, condition: Boolean): Unit
  def assertEquals(expected: Any, actual: Any): Unit
  def assertEquals(message: String, expected: Any, actual: Any): Unit
  def assertEquals(expected: Int, actual: Int): Unit
  def assertEquals(message: String, expected: Int, actual: Int): Unit
  def assertEquals(expected: Long, actual: Long): Unit
  def assertEquals(message: String, expected: Long, actual: Long): Unit
  def assertEquals(expected: Double, actual: Double, delta: Double): Unit
  def assertEquals(message: String, expected: Double, actual: Double, delta: Double): Unit
  def assertEquals(expected: Float, actual: Float, delta: Float): Unit
  def assertEquals(message: String, expected: Float, actual: Float, delta: Float): Unit
  def assertNotEquals(unexpected: Any, actual: Any): Unit
  def assertNotEquals(message: String, unexpected: Any, actual: Any): Unit
  def assertNull(obj: Any): Unit
  def assertNull(message: String, obj: Any): Unit
  def assertNotNull(obj: Any): Unit
  def assertNotNull(message: String, obj: Any): Unit
  def assertSame(expected: Any, actual: Any): Unit
  def assertSame(message: String, expected: Any, actual: Any): Unit
  def assertNotSame(unexpected: Any, actual: Any): Unit
  def assertNotSame(message: String, unexpected: Any, actual: Any): Unit
  def assertThrows[T <: Throwable](expectedThrowable: Class[T], runnable: ThrowingRunnable): T
  def assertThrows[T <: Throwable](message: String, expectedThrowable: Class[T], runnable: ThrowingRunnable): T
  def assertThat[T](actual: T, matcher: Matcher[T]): Unit
  def assertThat[T](reason: String, actual: T, matcher: Matcher[T]): Unit
  def fail(): Unit
  def fail(message: String): Unit
}

Core Assertions

Hamcrest Matchers

Expressive matcher library for readable and composable test assertions with type safety and detailed failure messages.

object CoreMatchers {
  def is[T](value: T): Matcher[T]
  def is[T](matcher: Matcher[T]): Matcher[T]
  def not[T](matcher: Matcher[T]): Matcher[T]
  def nullValue(): Matcher[AnyRef]
  def notNullValue(): Matcher[AnyRef]
  def instanceOf[T](typ: Class[_]): Matcher[T]
  def any[T](typ: Class[T]): Matcher[T]
}

trait Matcher[T] {
  def matches(actual: AnyRef): Boolean
  def describeTo(description: Description): Unit
}

Hamcrest Matchers

Test Lifecycle Annotations

Annotations for controlling test execution flow including setup, teardown, and test identification.

class Test(expected: Class[_ <: Throwable] = classOf[Test.None], timeout: Long = 0L) 
  extends StaticAnnotation

object Test {
  final class None private () extends Throwable
}

class Before extends StaticAnnotation  
class After extends StaticAnnotation
class BeforeClass extends StaticAnnotation
class AfterClass extends StaticAnnotation
class Ignore(value: String = "") extends StaticAnnotation
class Rule extends StaticAnnotation
class ClassRule extends StaticAnnotation

Test Lifecycle

Assumptions and Conditional Testing

Assumption methods for conditional test execution that skip tests when preconditions are not met.

object Assume {
  def assumeTrue(b: Boolean): Unit
  def assumeTrue(message: String, b: Boolean): Unit
  def assumeFalse(b: Boolean): Unit
  def assumeNotNull(objects: AnyRef*): Unit
  def assumeThat[T](actual: T, matcher: Matcher[T]): Unit
  def assumeNoException(e: Throwable): Unit
}

Test Assumptions

Array Assertions

Specialized assertion methods for comparing arrays with support for different element types and floating-point precision.

object Assert {
  def assertArrayEquals(expecteds: Array[AnyRef], actuals: Array[AnyRef]): Unit
  def assertArrayEquals(expecteds: Array[Boolean], actuals: Array[Boolean]): Unit
  def assertArrayEquals(expecteds: Array[Int], actuals: Array[Int]): Unit
  def assertArrayEquals(expecteds: Array[Double], actuals: Array[Double], delta: Double): Unit
  def assertArrayEquals(expecteds: Array[Float], actuals: Array[Float], delta: Float): Unit
}

Array Assertions

Test Runners and Framework Integration

Runner framework for custom test execution strategies and SBT testing framework integration.

class JUnitFramework extends Framework {
  val name: String
  def fingerprints(): Array[Fingerprint]
  def runner(args: Array[String], remoteArgs: Array[String], testClassLoader: ClassLoader): Runner
}

class RunWith(value: Class[_ <: Runner]) extends StaticAnnotation

Test Runners

Exception Handling and Error Reporting

Enhanced exception classes for detailed test failure reporting with string comparisons and assumption violations.

class ComparisonFailure(message: String, expected: String, actual: String) extends AssertionError {
  def getExpected(): String
  def getActual(): String  
}

class AssumptionViolatedException(message: String) extends RuntimeException

Exception Handling

Types

Core Test Types

trait ThrowingRunnable {
  def run(): Unit
}

class OptionalThrowable(throwable: Throwable = null) {
  def isPresent: Boolean
  def get(): Throwable
}

Framework Integration Types

trait Framework {
  def name: String
  def fingerprints(): Array[Fingerprint]
  def runner(args: Array[String], remoteArgs: Array[String], testClassLoader: ClassLoader): Runner
  def slaveRunner(args: Array[String], remoteArgs: Array[String], testClassLoader: ClassLoader, send: String => Unit): Runner
}

abstract class Runner {
  def done(): String
  def tasks(taskDefs: Array[TaskDef]): Array[Task]
}

abstract class ParentRunner[T](testClass: Class[_]) extends Runner

class BlockJUnit4ClassRunner(testClass: Class[_]) extends ParentRunner[FrameworkMethod]

final class JUnit4(klass: Class[_]) extends BlockJUnit4ClassRunner

trait Task {
  def taskDef(): TaskDef
  def execute(eventHandler: EventHandler, loggers: Array[Logger]): Array[Task]
}

trait TaskDef {
  def fullyQualifiedName(): String
  def fingerprint(): Fingerprint
  def explicitlySpecified(): Boolean
  def selectors(): Array[Selector]
}

trait Fingerprint {
  def requireNoArgConstructor(): Boolean
  def isModule(): Boolean
}

trait AnnotatedFingerprint extends Fingerprint {
  def annotationName(): String
}

class FrameworkMethod