I share data between ScalaTest suites using a common singleton Object. This works fine when running on JVM, but to my surprise it fails on Scala.js. It seems on Scala.js a fresh instance of the program is used for each suite and the singletons are not shared.
Consider following code:
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
object SharedTestData {
var useCount = 0
def useMe(): Int = {
useCount += 1
useCount
}
}
class ATest extends AnyFlatSpec with Matchers {
"This" should "pass" in {
val used = SharedTestData.useMe()
info(s"Used $used times")
}
}
class BTest extends AnyFlatSpec with Matchers {
"This" should "pass as well" in {
val used = SharedTestData.useMe()
info(s"Used $used times")
}
}
The output from sbt test
with project configured as Scala.js is:
[info] BTest:
[info] This
[info] - should pass as well
[info] + Used 1 times
[info] ATest:
[info] This
[info] - should pass
[info] + Used 1 times
[info] Run completed in 422 milliseconds.
[info] Total number of tests run: 2
Is there any technique which would allow me to share data between test suites on Scala.js? Some of the data take several minutes to compute and it is annoying computing them repeatedly.