summaryrefslogtreecommitdiff
path: root/examples/scala-js/tools/js/src/test/scala/scala/scalajs/tools/js/test/JasmineReporter.scala
blob: 7b63871e9d74a9ecc7957ec8a1ed421da35cb83e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package scala.scalajs.tools.js.test

import org.scalajs.jasmine.Suite

import org.scalajs.jasminetest._

import scala.scalajs.js.annotation.JSExport

import scala.scalajs.testbridge._

object JSConsoleTestOutput extends TestOutput {

  type Color = Null

  val errorColor: Color = null
  val successColor: Color = null
  val infoColor: Color = null

  def color(message: String, color: Color): String = message

  def error(message: String, stack: Array[StackTraceElement]): Unit =
    withStack(message, stack)

  def error(message: String): Unit = println(message)

  def failure(message: String, stack: Array[StackTraceElement]): Unit =
    withStack(message, stack)

  def failure(message: String): Unit = println(message)
  def succeeded(message: String): Unit = println(message)
  def skipped(message: String): Unit = println(message)
  def pending(message: String): Unit = println(message)
  def ignored(message: String): Unit = println(message)
  def canceled(message: String): Unit = println(message)

  object log extends TestOutputLog {
    def info(message: String): Unit = println(message)
    def warn(message: String): Unit = println(message)
    def error(message: String): Unit = println(message)
  }

  private def withStack(message: String, stack: Array[StackTraceElement]) =
    println(message + stack.mkString("\n", "\n", ""))

}

@JSExport("scalajs.JasmineConsoleReporter")
class JasmineConsoleReporter(throwOnFail: Boolean = false)
    extends JasmineTestReporter(JSConsoleTestOutput) {

  private var suiteFails: Int = 0
  private var suiteCount: Int = 0

  override def reportSuiteResults(suite: Suite): Unit = {
    super.reportSuiteResults(suite)
    if (suite.results().failedCount > 0)
      suiteFails += 1
    suiteCount += 1
  }

  override def reportRunnerResults(): Unit = {
    super.reportRunnerResults()
    val failed = suiteFails > 0
    val resStr = if (failed) "Failed" else "Passed"
    println(s"$resStr: Total $suiteCount, Failed $suiteFails")

    if (failed && throwOnFail)
      sys.error("Jasmine test suite failed.")
  }

}