Skip to content

Commit 6449e96

Browse files
committed
Scala.js: Test the Scala.js JUnit test suite in ESModule mode.
This ensures that the module-only features are tested.
1 parent 61519a3 commit 6449e96

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

.github/workflows/ci.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ jobs:
122122

123123
- name: Cmd Tests
124124
run: |
125-
./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"
125+
./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"
126126
./project/scripts/cmdTests
127127
./project/scripts/bootstrappedOnlyCmdTests
128128
129+
- name: Scala.js Test
130+
run: |
131+
./project/scripts/sbt ";sjsSandbox/run ;sjsSandbox/test ;sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
132+
129133
test_windows_fast:
130134
runs-on: [self-hosted, Windows]
131135
if: "(
@@ -167,7 +171,7 @@ jobs:
167171
shell: cmd
168172

169173
- name: Scala.js Test
170-
run: sbt ";sjsJUnitTests/test ;sjsCompilerTests/test"
174+
run: sbt ";sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
171175
shell: cmd
172176

173177
test_windows_full:
@@ -193,7 +197,7 @@ jobs:
193197
shell: cmd
194198

195199
- name: Scala.js Test
196-
run: sbt ";sjsJUnitTests/test ;sjsCompilerTests/test"
200+
run: sbt ";sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
197201
shell: cmd
198202

199203
mima:
@@ -464,10 +468,14 @@ jobs:
464468

465469
- name: Test
466470
run: |
467-
./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"
471+
./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"
468472
./project/scripts/cmdTests
469473
./project/scripts/bootstrappedOnlyCmdTests
470474
475+
- name: Scala.js Test
476+
run: |
477+
./project/scripts/sbt ";sjsSandbox/run ;sjsSandbox/test ;sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
478+
471479
publish_nightly:
472480
runs-on: [self-hosted, Linux]
473481
container:

project/Build.scala

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ import sbtbuildinfo.BuildInfoPlugin.autoImport._
2424

2525
import scala.util.Properties.isJavaAtLeast
2626
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._
27-
import org.scalajs.linker.interface.ModuleInitializer
27+
import org.scalajs.linker.interface.{ModuleInitializer, StandardConfig}
2828

2929
object DottyJSPlugin extends AutoPlugin {
3030
import Build._
3131

32+
object autoImport {
33+
val switchToESModules: StandardConfig => StandardConfig =
34+
config => config.withModuleKind(ModuleKind.ESModule)
35+
}
36+
37+
val writePackageJSON = taskKey[Unit](
38+
"Write package.json to configure module type for Node.js")
39+
3240
override def requires: Plugins = ScalaJSPlugin
3341

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

5260
// Typecheck the Scala.js IR found on the classpath
5361
scalaJSLinkerConfig ~= (_.withCheckIR(true)),
62+
63+
Compile / jsEnvInput := (Compile / jsEnvInput).dependsOn(writePackageJSON).value,
64+
Test / jsEnvInput := (Test / jsEnvInput).dependsOn(writePackageJSON).value,
65+
66+
writePackageJSON := {
67+
val packageType = scalaJSLinkerConfig.value.moduleKind match {
68+
case ModuleKind.NoModule => "commonjs"
69+
case ModuleKind.CommonJSModule => "commonjs"
70+
case ModuleKind.ESModule => "module"
71+
}
72+
73+
val path = target.value / "package.json"
74+
75+
IO.write(path, s"""{"type": "$packageType"}\n""")
76+
},
5477
)
5578
}
5679

@@ -1202,6 +1225,19 @@ object Build {
12021225
// A first blacklist of tests for those that do not compile or do not link
12031226
(Test / managedSources) ++= {
12041227
val dir = fetchScalaJSSource.value / "test-suite"
1228+
1229+
val linkerConfig = scalaJSStage.value match {
1230+
case FastOptStage => (Test / fastLinkJS / scalaJSLinkerConfig).value
1231+
case FullOptStage => (Test / fullLinkJS / scalaJSLinkerConfig).value
1232+
}
1233+
1234+
val moduleKind = linkerConfig.moduleKind
1235+
val hasModules = moduleKind != ModuleKind.NoModule
1236+
1237+
def conditionally(cond: Boolean, subdir: String): Seq[File] =
1238+
if (!cond) Nil
1239+
else (dir / subdir ** "*.scala").get
1240+
12051241
(
12061242
(dir / "shared/src/test/scala" ** (("*.scala": FileFilter)
12071243
-- "ReflectiveCallTest.scala" // uses many forms of structural calls that are not allowed in Scala 3 anymore
@@ -1220,9 +1256,28 @@ object Build {
12201256
++ (dir / "js/src/test/require-2.12" ** "*.scala").get
12211257
++ (dir / "js/src/test/require-sam" ** "*.scala").get
12221258
++ (dir / "js/src/test/scala-new-collections" ** "*.scala").get
1223-
++ (dir / "js/src/test/require-no-modules" ** "*.scala").get
1259+
1260+
++ conditionally(!hasModules, "js/src/test/require-no-modules")
1261+
++ conditionally(hasModules, "js/src/test/require-modules")
1262+
++ conditionally(hasModules && !linkerConfig.closureCompiler, "js/src/test/require-multi-modules")
1263+
++ conditionally(moduleKind == ModuleKind.ESModule, "js/src/test/require-dynamic-import")
1264+
++ conditionally(moduleKind == ModuleKind.ESModule, "js/src/test/require-esmodule")
12241265
)
12251266
},
1267+
1268+
Test / managedResources ++= {
1269+
val testDir = fetchScalaJSSource.value / "test-suite/js/src/test"
1270+
1271+
val common = (testDir / "resources" ** "*.js").get
1272+
1273+
val moduleSpecific = scalaJSLinkerConfig.value.moduleKind match {
1274+
case ModuleKind.NoModule => Nil
1275+
case ModuleKind.CommonJSModule => (testDir / "resources-commonjs" ** "*.js").get
1276+
case ModuleKind.ESModule => (testDir / "resources-esmodule" ** "*.js").get
1277+
}
1278+
1279+
common ++ moduleSpecific
1280+
},
12261281
)
12271282

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

0 commit comments

Comments
 (0)