Skip to content

Commit a18a770

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 a3c1a70 commit a18a770

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ 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;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;set sjsJUnitTests/scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) };sjsJUnitTests/test;sjsCompilerTests/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
@@ -167,7 +167,7 @@ jobs:
167167
shell: cmd
168168

169169
- name: Scala.js Test
170-
run: sbt ";sjsJUnitTests/test ;sjsCompilerTests/test"
170+
run: sbt ";sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) } ;sjsJUnitTests/test ;sjsCompilerTests/test"
171171
shell: cmd
172172

173173
test_windows_full:
@@ -193,7 +193,7 @@ jobs:
193193
shell: cmd
194194

195195
- name: Scala.js Test
196-
run: sbt ";sjsJUnitTests/test ;sjsCompilerTests/test"
196+
run: sbt ";sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) } ;sjsJUnitTests/test ;sjsCompilerTests/test"
197197
shell: cmd
198198

199199
mima:
@@ -464,7 +464,7 @@ jobs:
464464

465465
- name: Test
466466
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"
467+
./project/scripts/sbt ";dist/pack ;scala3-bootstrapped/compile ;scala3-bootstrapped/test;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;set sjsJUnitTests/scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) };sjsJUnitTests/test;sjsCompilerTests/test ;sbt-test/scripted scala2-compat/* ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test"
468468
./project/scripts/cmdTests
469469
./project/scripts/bootstrappedOnlyCmdTests
470470

project/Build.scala

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import org.scalajs.linker.interface.ModuleInitializer
2929
object DottyJSPlugin extends AutoPlugin {
3030
import Build._
3131

32+
val writePackageJSON = taskKey[Unit](
33+
"Write package.json to configure module type for Node.js")
34+
3235
override def requires: Plugins = ScalaJSPlugin
3336

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

5255
// Typecheck the Scala.js IR found on the classpath
5356
scalaJSLinkerConfig ~= (_.withCheckIR(true)),
57+
58+
Compile / jsEnvInput := (Compile / jsEnvInput).dependsOn(writePackageJSON).value,
59+
Test / jsEnvInput := (Test / jsEnvInput).dependsOn(writePackageJSON).value,
60+
61+
writePackageJSON := {
62+
val packageType = scalaJSLinkerConfig.value.moduleKind match {
63+
case ModuleKind.NoModule => "commonjs"
64+
case ModuleKind.CommonJSModule => "commonjs"
65+
case ModuleKind.ESModule => "module"
66+
}
67+
68+
val path = target.value / "package.json"
69+
70+
IO.write(path, s"""{"type": "$packageType"}\n""")
71+
},
5472
)
5573
}
5674

@@ -1202,6 +1220,19 @@ object Build {
12021220
// A first blacklist of tests for those that do not compile or do not link
12031221
(Test / managedSources) ++= {
12041222
val dir = fetchScalaJSSource.value / "test-suite"
1223+
1224+
val linkerConfig = scalaJSStage.value match {
1225+
case FastOptStage => (Test / fastLinkJS / scalaJSLinkerConfig).value
1226+
case FullOptStage => (Test / fullLinkJS / scalaJSLinkerConfig).value
1227+
}
1228+
1229+
val moduleKind = linkerConfig.moduleKind
1230+
val hasModules = moduleKind != ModuleKind.NoModule
1231+
1232+
def conditionally(cond: Boolean, subdir: String): Seq[File] =
1233+
if (!cond) Nil
1234+
else (dir / subdir ** "*.scala").get
1235+
12051236
(
12061237
(dir / "shared/src/test/scala" ** (("*.scala": FileFilter)
12071238
-- "ReflectiveCallTest.scala" // uses many forms of structural calls that are not allowed in Scala 3 anymore
@@ -1220,9 +1251,21 @@ object Build {
12201251
++ (dir / "js/src/test/require-2.12" ** "*.scala").get
12211252
++ (dir / "js/src/test/require-sam" ** "*.scala").get
12221253
++ (dir / "js/src/test/scala-new-collections" ** "*.scala").get
1223-
++ (dir / "js/src/test/require-no-modules" ** "*.scala").get
1254+
1255+
++ conditionally(!hasModules, "js/src/test/require-no-modules")
1256+
++ conditionally(hasModules, "js/src/test/require-modules")
12241257
)
12251258
},
1259+
1260+
Test / managedResources ++= {
1261+
val testDir = fetchScalaJSSource.value / "test-suite/js/src/test"
1262+
1263+
scalaJSLinkerConfig.value.moduleKind match {
1264+
case ModuleKind.NoModule => Nil
1265+
case ModuleKind.CommonJSModule => (testDir / "resources-commonjs" ** "*.js").get
1266+
case ModuleKind.ESModule => (testDir / "resources-esmodule" ** "*.js").get
1267+
}
1268+
},
12261269
)
12271270

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

0 commit comments

Comments
 (0)