Repository navigation
javascript window object not available in test #66
|
Hi, I'm trying to make a test suite for Tauri SQL plugin. Currently using mill 0.12.9 and following is my build.sc import mill._, scalalib._, scalajslib._
import mill.scalajslib.api._
import $ivy.`io.github.nafg.millbundler::jsdeps::0.3.0`
import $ivy.`io.github.nafg.millbundler::millbundler::0.3.0`
import io.github.nafg.millbundler._
import io.github.nafg.millbundler.jsdeps._
object allog extends ScalaJSRollupModule {
def scalaVersion = "3.7.2"
def scalaJSVersion = "1.19.0"
def ivyDeps = Agg(
ivy"com.lihaoyi::scalatags::0.13.1",
ivy"org.scala-js::scalajs-dom::2.8.1",
ivy"org.typelevel::cats-effect::3.6.3",
// mvn"org.scalablytyped::tauri-apps__api::2.5.0-13cef9",
ivy"org.scalablytyped::tauri-apps__api::2.8.0-4a6bfa",
ivy"org.scalablytyped::tauri-apps__plugin-sql::2.3.0-f082ca",
)
def jsDeps =
super.jsDeps() ++
JsDeps(
dependencies = Map(
// "@tauri-apps/api" -> "^2.2.0"
"@tauri-apps/plugin-sql" -> "~2.3.0"
)
)
def fl = Task {
val jsPath = fastLinkJS().dest.path
val targetPath = jsPath / os.up / os.up / os.up / "src"
val target = targetPath / "main.js"
os.copy.over(jsPath / "main.js", target)
os.copy.over(jsPath / "main.js.map", targetPath / "main.js.map")
PathRef(target)
}
def dev = Task {
val bundles = allog.devBundle()
val jsPath = bundles.head.path / os.up
val targetPath = jsPath / os.up / os.up / os.up / "src"
val target = targetPath / "main.js"
os.copy.over(jsPath / "out-bundle.js", target)
val sourceMap = jsPath / "out-bundle.js.map"
if (os.exists(sourceMap)) {
os.copy.over(sourceMap, targetPath / "main.js.map")
}
PathRef(target)
}
override def moduleKind = ModuleKind.ESModule
object test extends ScalaJSTests with ScalaJSRollupModule.Test with TestModule.Weaver {
def ivyDeps = Agg(
ivy"org.typelevel::weaver-cats::0.9.3"
)
def testFramework = "weaver.framework.CatsEffect"
}
} |
Replies: 1 comment 1 reply
|
What’s happening here is that your tests are being run under a plain JS environment (via Node through Scala.js), but the Tauri SQL plugin expects to be running inside a Tauri runtime where window.TAURI_INTERNALS is defined. ReferenceError: window is not defined because Node doesn’t have a window object. you can try these
import scala.scalajs.js @js.native // In your test bootstrap That way, window.TAURI_INTERNALS.invoke(...) exists during tests and won’t crash.
Instead of Node, you can run Scala.js tests under a headless browser (like jsdom). object test extends ScalaJSTests with ScalaJSRollupModule.Test with TestModule.Weaver {
If your goal is to actually call the real @tauri-apps/plugin-sql inside Tauri, you’ll need to run tests inside a Tauri app (not via Scala.js test runner). In that case, unit tests should mock things (option 1), and you do true integration testing separately in Tauri. |
What’s happening here is that your tests are being run under a plain JS environment (via Node through Scala.js), but the Tauri SQL plugin expects to be running inside a Tauri runtime where window.TAURI_INTERNALS is defined.
ReferenceError: window is not defined because Node doesn’t have a window object.
you can try these
if you only need to exercise your own Scala.js code (not the actual Tauri runtime), you can provide a fake window during testing:
import scala.scalajs.js
import scala.scalajs.js.annotation._
@js.native
@JSGlobalScope
object Globals extends js.Object {
var window: js.Dynamic = js.native
}
// In your test bootstrap
Globals.window = js.Dynamic.literal(
…