Skip to content

Commit

Permalink
Scala.js: Test the Scala.js JUnit test suite in ESModule mode.
Browse files Browse the repository at this point in the history
This ensures that the module-only features are tested.
  • Loading branch information
sjrd committed Jul 21, 2022
1 parent a3c1a70 commit 030865f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ jobs:

- name: Cmd Tests
run: |
./project/scripts/sbt ";dist/pack; scala3-bootstrapped/compile; scala3-bootstrapped/test;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;sjsCompilerTests/test ;sbt-test/scripted scala2-compat/* ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test; scala3-compiler-bootstrapped/scala3CompilerCoursierTest:test"
./project/scripts/sbt ";dist/pack; scala3-bootstrapped/compile; scala3-bootstrapped/test ;sbt-test/scripted scala2-compat/* ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test; scala3-compiler-bootstrapped/scala3CompilerCoursierTest:test"
./project/scripts/cmdTests
./project/scripts/bootstrappedOnlyCmdTests
- name: Scala.js Test
run: |
./project/scripts/sbt ";sjsSandbox/run ;sjsSandbox/test ;sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
test_windows_fast:
runs-on: [self-hosted, Windows]
if: "(
Expand Down Expand Up @@ -167,7 +171,7 @@ jobs:
shell: cmd

- name: Scala.js Test
run: sbt ";sjsJUnitTests/test ;sjsCompilerTests/test"
run: sbt ";sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
shell: cmd

test_windows_full:
Expand All @@ -193,7 +197,7 @@ jobs:
shell: cmd

- name: Scala.js Test
run: sbt ";sjsJUnitTests/test ;sjsCompilerTests/test"
run: sbt ";sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
shell: cmd

mima:
Expand Down Expand Up @@ -464,10 +468,14 @@ jobs:

- name: Test
run: |
./project/scripts/sbt ";dist/pack ;scala3-bootstrapped/compile ;scala3-bootstrapped/test;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;sjsCompilerTests/test ;sbt-test/scripted scala2-compat/* ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test"
./project/scripts/sbt ";dist/pack ;scala3-bootstrapped/compile ;scala3-bootstrapped/test ;sbt-test/scripted scala2-compat/* ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test"
./project/scripts/cmdTests
./project/scripts/bootstrappedOnlyCmdTests
- name: Scala.js Test
run: |
./project/scripts/sbt ";sjsSandbox/run ;sjsSandbox/test ;sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
publish_nightly:
runs-on: [self-hosted, Linux]
container:
Expand Down
54 changes: 52 additions & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ import sbtbuildinfo.BuildInfoPlugin.autoImport._

import scala.util.Properties.isJavaAtLeast
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._
import org.scalajs.linker.interface.ModuleInitializer
import org.scalajs.linker.interface.{ModuleInitializer, StandardConfig}

object DottyJSPlugin extends AutoPlugin {
import Build._

object autoImport {
val switchToESModules: StandardConfig => StandardConfig =
config => config.withModuleKind(ModuleKind.ESModule)
}

val writePackageJSON = taskKey[Unit](
"Write package.json to configure module type for Node.js")

override def requires: Plugins = ScalaJSPlugin

override def projectSettings: Seq[Setting[_]] = Def.settings(
Expand All @@ -51,6 +59,21 @@ object DottyJSPlugin extends AutoPlugin {

// Typecheck the Scala.js IR found on the classpath
scalaJSLinkerConfig ~= (_.withCheckIR(true)),

Compile / jsEnvInput := (Compile / jsEnvInput).dependsOn(writePackageJSON).value,
Test / jsEnvInput := (Test / jsEnvInput).dependsOn(writePackageJSON).value,

writePackageJSON := {
val packageType = scalaJSLinkerConfig.value.moduleKind match {
case ModuleKind.NoModule => "commonjs"
case ModuleKind.CommonJSModule => "commonjs"
case ModuleKind.ESModule => "module"
}

val path = target.value / "package.json"

IO.write(path, s"""{"type": "$packageType"}\n""")
},
)
}

Expand Down Expand Up @@ -1202,6 +1225,19 @@ object Build {
// A first blacklist of tests for those that do not compile or do not link
(Test / managedSources) ++= {
val dir = fetchScalaJSSource.value / "test-suite"

val linkerConfig = scalaJSStage.value match {
case FastOptStage => (Test / fastLinkJS / scalaJSLinkerConfig).value
case FullOptStage => (Test / fullLinkJS / scalaJSLinkerConfig).value
}

val moduleKind = linkerConfig.moduleKind
val hasModules = moduleKind != ModuleKind.NoModule

def conditionally(cond: Boolean, subdir: String): Seq[File] =
if (!cond) Nil
else (dir / subdir ** "*.scala").get

(
(dir / "shared/src/test/scala" ** (("*.scala": FileFilter)
-- "ReflectiveCallTest.scala" // uses many forms of structural calls that are not allowed in Scala 3 anymore
Expand All @@ -1220,9 +1256,23 @@ object Build {
++ (dir / "js/src/test/require-2.12" ** "*.scala").get
++ (dir / "js/src/test/require-sam" ** "*.scala").get
++ (dir / "js/src/test/scala-new-collections" ** "*.scala").get
++ (dir / "js/src/test/require-no-modules" ** "*.scala").get

++ conditionally(!hasModules, "js/src/test/require-no-modules")
++ conditionally(hasModules, "js/src/test/require-modules")
++ conditionally(moduleKind == ModuleKind.ESModule, "js/src/test/require-dynamic-import")
++ conditionally(moduleKind == ModuleKind.ESModule, "js/src/test/require-esmodule")
)
},

Test / managedResources ++= {
val testDir = fetchScalaJSSource.value / "test-suite/js/src/test"

scalaJSLinkerConfig.value.moduleKind match {
case ModuleKind.NoModule => Nil
case ModuleKind.CommonJSModule => (testDir / "resources-commonjs" ** "*.js").get
case ModuleKind.ESModule => (testDir / "resources-esmodule" ** "*.js").get
}
},
)

lazy val sjsCompilerTests = project.in(file("sjs-compiler-tests")).
Expand Down

0 comments on commit 030865f

Please sign in to comment.