jhegedus
jhegedus

Reputation: 20653

async before in scalatest for scalajs

In the code example below, how can I wait for ajaxCall() to finish before starting test 1 when using scalatest to test Scala.js code ? I cannot use await in Scala.js.

class ClientGetEntityDynTest
    extends AsyncFunSuite
    with Matchers
    with BeforeAndAfter {

  implicit override def executionContext =
    scala.scalajs.concurrent.JSExecutionContext.Implicits.queue

  before {
    ajaxCall(...) // returns Future[...]
    ... // I would like to wait for ajaxCall to finish before starting test 1
  }

  test("test 1") {
    ...
    getEntityDyn(...) // returns Future[Assertion]
  }
}

This one year old issue seems to be related but not really resolved.

One simple possibility would be to make my own testWithBefore method... that calls test and waits for a Future to complete before calling test but maybe it is possible to do this without this workaround.

Upvotes: 3

Views: 586

Answers (1)

Justin du Coeur
Justin du Coeur

Reputation: 2659

I suspect you need to restructure your tests, to not use BeforeAndAfter. I'm not sure of the best solution, but the fall-back would be to create your own higher-order function, called something like beforeAsync(fun: => Future[Any]), and manually use that in your tests.

I suspect it wouldn't be too hard to take BeforeAndAfter.scala, and create a variant BeforeAndAfterAsyc that has this beforeAsync() function in it, but I haven't tried doing so.

Upvotes: 4

Related Questions