In this section we will discuss how to test Scala.js code managed as Full cross project. All namesfor example subproject name such as fooJVM are taken fromcross compile example.

Directory Structure

  1. <project root>
  2. +- jvm
  3. | +- src/main/scala
  4. | +- src/test/scala
  5. +- js
  6. | +- src/main/scala
  7. | +- src/test/scala
  8. +- shared
  9. +- src/main/scala
  10. +- src/test/scala

Similarly to {shared|js|jvm}/src/main/scala folders that contain your code, there can be also{shared|js|jvm}/src/test/scala folders that would contain your tests. Tests that verify correctness of shared codeshould go to shared test folder, while tests that check Scala JVM code or Scala.js code only should go to jvm orjs test folders respectively. Calling sbt> fooJVM/test will execute all tests residing in shared and jvmfolders, thus testing Scala JVM code. Calling sbt> fooJS/test will execute all tests residing in shared and jsfolders, so Scala.js code is tested. In case of the full cross project, root project aggregates JVM and JSparts, so when calling sbt> test will effectively run both fooJVM/test and fooJS/test.

Integration testing

Configuring a regular non-Scala.js sbt project to have it:test isdocumented in sbt.For a Scala.js project, you will also need to install the Scala.js-specific settings and tasks to the it configuration, as follows:

  1. lazy val myProject = project.in(file(".")).
  2. enablePlugins(ScalaJSPlugin).
  3. // add the `it` configuration
  4. configs(IntegrationTest).
  5. // add `it` tasks
  6. settings(Defaults.itSettings: _*).
  7. // add Scala.js-specific settings and tasks to the `it` configuration
  8. settings(inConfig(IntegrationTest)(ScalaJSPlugin.testConfigSettings): _*).
  9. ...

For a crossProject, you also need to setup the shared/src/it/scala source directory.The complete setup is as follows:

  1. lazy val cross = crossProject.in(file(".")).
  2. // add the `it` configuration
  3. configs(IntegrationTest).
  4. // add `it` tasks
  5. settings(Defaults.itSettings: _*).
  6. // add Scala.js-specific settings and tasks to the `it` configuration
  7. jsSettings(inConfig(IntegrationTest)(ScalaJSPlugin.testConfigSettings): _*).
  8. // add the `shared` folder to source directories
  9. settings(
  10. unmanagedSourceDirectories in IntegrationTest ++=
  11. CrossType.Full.sharedSrcDir(baseDirectory.value, "it").toSeq
  12. ).
  13. ...

Now you can put tests in {shared|jvm|js}/src/it/scala and they will run when you call sbt> it:test. Similarly totest, there is also two iterations and they can be executed separately by explicitely stating jvm or js project name,for example sbt> fooJS/it:test.

Testing frameworks

A list of testing frameworks compatible with Scala.js can be found here.

Note: Don’t forget to mark a test framework SBT dependency as test,it if you have both unit and integration tests.

Testing over fullOptJS-generated files

By default, tests runs over fastOptJS-built JS files since their build time are shorter than fullOptJS.

If you want to run tests over fullOptJS-build JS files for some reason, run set scalaJSStage in Global := FullOptStage before test.This increases test time significantly, and omit checks for undefined behavior, so not recommended in default build settings. Instead, consider run test both in FastOptStage and FullOptStage in CI.