scalajs-bundler icon indicating copy to clipboard operation
scalajs-bundler copied to clipboard

`testHtmlFastOpt` and `testHtmlFullOpt` use incorrect file

Open dispalt opened this issue 8 years ago • 22 comments

The HTML that gets generated uses [name]-test-[stage].js vs [name]-test-[stage]-bundle.js. It looks like it's looking at fastOptJS in testHtmlFastOpt for the file name. I made a quick fix in my own project, and would contribute it back but I don't really see where the bundle is named. Obviously it's fairly hacky.

fastOptJS in testHtmlFastOpt in Test := {
      val f = (fastOptJS in testHtmlFastOpt in Test).value
      val fd = new File(f.data.getPath.stripSuffix(".js") + "-bundle.js")
      f.copy(fd)(f.metadata)
    }

dispalt avatar Jan 11 '17 05:01 dispalt

Thanks for reporting. The bundle files are returned by the webpack command. I think the right implementation would be something like the following:

fastOptJS in testHtmlFastOpt in Test := {
  (webpack in fastOptJS).value match {
    case Seq(bundle) => Attributed(bundle)((fastOptJS in testHtmlFastOpt).value.metadata)
    case _ => sys.error("testHtmlFastOpt task supports only exactly one bundle")
  }
}

What do you think? (Or maybe just Attributed.blank(bundle))

julienrf avatar Jan 11 '17 09:01 julienrf

I like this much better, I haven't tested it though. Shall I make the change on the website?

dispalt avatar Jan 11 '17 16:01 dispalt

What do you mean by “the website”?

julienrf avatar Jan 11 '17 16:01 julienrf

In the cookbooks sections. Or, is this something that you fix in the plugin?

dispalt avatar Jan 11 '17 16:01 dispalt

That would be great if you could directly fix the plugin :)

You should add it to this list (and you should add a similar setting for testHtmlFullOpt, I think) and then add the following lines > testHtmlFastOpt and > testHtmlFullOpt at the end of this file.

julienrf avatar Jan 11 '17 16:01 julienrf

On it!

dispalt avatar Jan 11 '17 16:01 dispalt

Is this related to https://github.com/scalacenter/scalajs-bundler/issues/38 ? I'm getting "ReferenceError: exports is not defined" when trying to run tests with scala js bundler and jsdom... It seems that both tickets refer to this issue?

raquo avatar Feb 02 '17 06:02 raquo

@raquo Are you using the testHtmlFastOpt task? Just running the tests with test should work if you added requiresDOM in Test := true to your build.

julienrf avatar Feb 02 '17 09:02 julienrf

Thanks! I think I was using the test command with jsDependencies += RuntimeDOM % Test instead of the requiresDOM line you mentioned. Although I did try testHtmlFastOpt with the same result. Eventually I found this example https://github.com/scalacenter/scalajs-bundler/tree/master/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/static and now I'm using the config as you've suggested, and it works.

raquo avatar Feb 06 '17 22:02 raquo

I'm now getting the same exports is not defined problem when trying to run tests with selenium using the test command. Is that this same bug manifesting, or does this look like an issue with scalajs-env-selenium? Or maybe I'm doing something wrong?

The relevant build.sbt part is:

persistLauncher in Test := false

requiresDOM in Test := true

jsEnv in Test := new org.scalajs.jsenv.selenium.SeleniumJSEnv(org.scalajs.jsenv.selenium.Chrome())

(without the last line it works fine)

The workaround mentioned in the first post doesn't have any effect, I'm guessing because testHtmlFastOpt command is not run. Replacing fastOptJS in testHtmlFastOpt in Test with fastOptJS in Test also doesn't work as this creates incorrectly names files (-bundle-bundle.js)

PS sorry for hijacking this issue, it seems like it might be the same problem.

raquo avatar Feb 21 '17 09:02 raquo

It might be the same issue, indeed… This would require a bit of investigation.

julienrf avatar Feb 21 '17 09:02 julienrf

I am also getting the same exports is not defined error with jsEnv in Test := new org.scalajs.jsenv.selenium.SeleniumJSEnv(org.scalajs.jsenv.selenium.Chrome()).

ramnivas avatar Jan 10 '18 21:01 ramnivas

Getting same problem with @ramnivas

mucahitkantepe avatar Feb 09 '18 09:02 mucahitkantepe

I am getting the same Problem in my project

atooni avatar Aug 17 '18 15:08 atooni

@raquo Hmm why would you have requiresDOM in Test if using Selenium as your JSEnv? Doesn't the latter provide that inherently? I thought the former's role was solely to download jsdom module.

matthughes avatar Feb 21 '19 16:02 matthughes

If you compare the loaders for the web app vs a test you see the following:

runner

var exports = window;
exports.require = window["ScalaJSBundlerLibrary"].require;

test

{
  var x0 = require("/project/target/scala-2.12/scalajs-bundler/test/project-test-fastopt.js");
  Object.keys(x0).forEach((function(x1) {
    return window[x1] = x0[x1]
  }))
}

Why do we define exports in the one but not the other?

matthughes avatar Feb 21 '19 17:02 matthughes

I also run into this issue. I am unable to run any tests now. It is not related to Selenium. it looks like what @matthughes said that the test loader is not including i.e. the -bundle version?

fkoehler avatar Jun 21 '19 11:06 fkoehler

In my very specific case I was able to fix the error by removing this line:

jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()

and adding this one:

requireJsDomEnv in Test := true

Then a correct codeWithJSDOMContext.js was created in scalajs-bundler-jsdom and now the tests work.

fkoehler avatar Jun 21 '19 16:06 fkoehler

Has there been any progress on this? Facing the same issue (ReferenceError: exports is not defined) and requireJsDomEnv in Test := true does not seem to fix it.

fbaierl avatar May 15 '20 08:05 fbaierl

I believe this should be fixed when using Scala.js 1.x, with

requireJsDomEnv in Test := true

and with the task testHtml (which replaces testHtmlFastOpt and testHtmlFullOpt of Scala.js 0.6.x).

sjrd avatar May 15 '20 08:05 sjrd

Alright thanks. I am still using Scala.js 0.6 so I guess I'll have to try to update. :)

fbaierl avatar May 15 '20 09:05 fbaierl

Unfortunately we cannot update to Scala.js 1.0.1 yet since too many dependencies are still on 0.6. I'll need to look for a different solution. Thanks anyway @sjrd .

fbaierl avatar May 15 '20 11:05 fbaierl