-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestNode.scala
39 lines (29 loc) · 1.24 KB
/
TestNode.scala
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
package japgolly.webapputil.test.node
import japgolly.microlibs.testutil.TestUtil._
import japgolly.scalajs.react.AsyncCallback
import org.scalajs.dom.Crypto
import scala.concurrent.Future
import scala.scalajs.js
/** Node JS access provided by `project/AdvancedNodeJSEnv.scala`. */
trait TestNode {
@inline def window = js.Dynamic.global.window
@inline def node = window.node
def require(path: String): js.Dynamic =
node.require(path).asInstanceOf[js.Dynamic]
def envVarGet(name: String): js.UndefOr[String] =
node.process.env.selectDynamic(name).asInstanceOf[js.UndefOr[String]]
def envVarNeed(name: String): String =
envVarGet(name).getOrElse(throw new RuntimeException("Missing env var: " + name))
val inCI = envVarGet("CI").contains("1")
var asyncTestTimeout = if (inCI) 60000 else 3000
def asyncTest[A](timeoutMs: Int = asyncTestTimeout)(ac: AsyncCallback[A]): Future[A] =
ac.timeoutMs(timeoutMs).map {
case Some(a) => a
case None => fail(s"Async test timed out after ${timeoutMs / 1000} sec.")
}.unsafeToFuture()
lazy val webCrypto: Crypto = {
// https://github.com/nodejs/node/pull/35093
require("crypto").webcrypto.asInstanceOf[Crypto]
}
}
object TestNode extends TestNode