Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Node.js requirement with Scala.js #376

Merged
merged 1 commit into from
Jul 2, 2021

Conversation

olafurpg
Copy link
Member

@olafurpg olafurpg commented Jun 7, 2021

Previously, it was not possible to use MUnit with browser-based
JavaScript environments because MUnit imported the Node.js fs module.
This commit refactors the code so that MUnit should gracefully recover
when the Node.js fs module is not available.

Fixes #247

Previously, it was not possible to use MUnit with browser-based
JavaScript environments because MUnit imported the Node.js `fs` module.
This commit refactors the code so that MUnit should gracefully recover
when the Node.js `fs` module is not available.
@@ -100,8 +100,7 @@ val sharedJVMSettings: List[Def.Setting[_]] = List(
) ++ mimaEnable
val sharedJSSettings: List[Def.Setting[_]] = List(
skipIdeaSettings,
crossScalaVersions := allScalaVersions.filterNot(_.startsWith("0.")),
scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule))
crossScalaVersions := allScalaVersions.filterNot(_.startsWith("0."))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
crossScalaVersions := allScalaVersions.filterNot(_.startsWith("0."))
crossScalaVersions := allScalaVersions

just as a complete side note, I don't think we need this anymore

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I had a draft comment saying the same thing

@raquo
Copy link

raquo commented Jun 7, 2021

Thanks, checked it out locally. It does not throw as before, but now MUnit does not print any information about the assertion failures except the name of the test in which they appeared.

Munit on a JVM project prints this kind of useful error on failure:

==> X com.raquo.mapk.v1.V1MapKSpec.xxx  0.016s munit.ComparisonFailException: <projectRoot>/src/test/scala/com/raquo/mapk/v1/V1MapKSpec.scala:32
31:  test("xxx") {
32:    assertEquals(true, false)
33:  }
values are not the same
=> Obtained
true
=> Diff (- obtained, + expected)
-true
+false

Whereas MUnit in this PR prints only the following on a similar test in a JS project:

==> X com.raquo.domtestutils.MUnitJsDomSpec.xxx 0.01s munit.ComparisonFailException:

Also, the console is now printing a warning (not an error) before the tests:

[info] Writing and bundling the test loader
[warn] ./domtestutils-test-fastopt.js 4434:33-48
[warn] Critical dependency: the request of a dependency is an expression
[warn]     at CommonJsRequireContextDependency.getWarnings (<projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/webpack/lib/dependencies/ContextDependency.js:40:18)
[warn]     at Compilation.reportDependencyErrorsAndWarnings (<projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/webpack/lib/Compilation.js:1360:24)
[warn]     at <projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/webpack/lib/Compilation.js:1168:10
[warn]     at AsyncSeriesHook.eval [as callAsync] (eval at create (<projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:13:1)
[warn]     at AsyncSeriesHook.lazyCompileHook (<projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/tapable/lib/Hook.js:154:20)
[warn]     at Compilation.finish (<projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/webpack/lib/Compilation.js:1163:28)
[warn]     at <projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/webpack/lib/Compiler.js:622:17
[warn]     at eval (eval at create (<projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)
[warn]     at <projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/webpack/lib/Compilation.js:1095:12
[warn]     at <projectRoot>/target/scala-2.13/scalajs-bundler/test/node_modules/webpack/lib/Compilation.js:1007:9
[warn]     at processTicksAndRejections (internal/process/task_queues.js:79:11)
[warn]  @ ./domtestutils-test-fastopt-loader.js

You mentioned in previous comments that you need the fs module to read the source files to display information about the failure. So... does this PR replace that logic with a stub if the fs module can't be loaded?

Comment on lines +62 to +63
// if (JSIO.isNode) JSPath.parse(JSPath.resolve()).root
// else "/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover?

@@ -100,8 +100,7 @@ val sharedJVMSettings: List[Def.Setting[_]] = List(
) ++ mimaEnable
val sharedJSSettings: List[Def.Setting[_]] = List(
skipIdeaSettings,
crossScalaVersions := allScalaVersions.filterNot(_.startsWith("0.")),
scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule))
crossScalaVersions := allScalaVersions.filterNot(_.startsWith("0."))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I had a draft comment saying the same thing

@olafurpg
Copy link
Member Author

olafurpg commented Jun 7, 2021

@raquo thank you for trying it out! What Scala.js linker settings are you using to reproduce that? It might require a small tweak to get the exception message

@raquo
Copy link

raquo commented Jun 8, 2021

@olafurpg Here is all of the linker config and a few more relevant bits:

scalaVersion := "2.13.5" // Scala.js version is 1.5.0
(Test / scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.CommonJSModule) })
(Test / requireJsDomEnv) := true
(installJsdom / version) := "16.4.0"
useYarn := true
(Test / parallelExecution) := false

No other scalaJSLinkerConfig settings (I also had source maps off, but commented that out, and still the same). I only added the line with CommonJSModule because MUnit docs told me to, but commenting it out does not change anything.

One weirdness on my side that I can think of is that my project where I'm trying to use this is a testing library, so it requires the MUnit dependency without appending % "test". Whereas my scalaJSLinkerConfig has a Test / prefix as you see above. So maybe something is not catching on? I tried removing that prefix or replacing it with Compile /, but that did not produce any effect.

For the record, the branch that I'm trying to build is here. The simple test that I made to fail is MUnitJsDomSpec. MUnit version is defined in Versions.sbt.

@armanbilge
Copy link
Contributor

@olafurpg I can confirm this PR works over at http4s/http4s#4938.

Thanks for making this change, I'm badly in need of this. Is there anything I can do to get this merged and (snapshot) released ASAP? I'm happy to polish up this PR as @gabro and @tgodzik see fit.

@gabro gabro merged commit be448cb into scalameta:main Jul 2, 2021
@olafurpg olafurpg mentioned this pull request Oct 14, 2021
armanbilge added a commit to armanbilge/munit that referenced this pull request Oct 16, 2021
olafurpg pushed a commit that referenced this pull request Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid relying on Node's fs when running in browser-based JSEnv
5 participants