Facing ReferenceError while running tests with ScalaJS Bundler

青春壹個敷衍的年華 提交于 2021-01-28 08:47:36

问题


I am facing this issue when upgrading from sbt-scalajs 0.6.x to 1.2.0 and the issue is:-

With sbt-scalajs v0.6.26 (and sbt-scalajs-bundler v0.14.0), I have enabled the jsdom support for tests:

requireJsDomEnv in Test := true

And test suites are running fine.

But with sbt-scalajs v1.2.0 (and sbt-scalajs-bundler v0.18.0), I have enabled the jsdom support for tests too:

requireJsDomEnv in Test := true

But this is giving me the following error:

[info] Writing and bundling the test loader
webpack:///./node_modules/@opentelemetry/api/build/src/platform/browser/globalThis.js?:21
exports._globalThis = typeof globalThis === 'object' ? globalThis : window;
                                                                    ^
ReferenceError: window is not defined
    at eval (webpack:///./node_modules/@opentelemetry/api/build/src/platform/browser/globalThis.js?:21:69)
    at Object../node_modules/@opentelemetry/api/build/src/platform/browser/globalThis.js

However, when I am removing the requireJsDomEnv in Test := true setting, the test suites run fine. I need to understand why this is happening? And how I can fix it?

I am using:

scala -> 2.13.3
Java -> OpenJDK 14.0.2

回答1:


From the code, it looks like you are using node-js env with JSDom.

So to enable building you have to use JSDOMNodeJSEnv() for example: -

requireJsDomEnv in Test := true
jsEnv in Test := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()

Here the doc for more information:- https://www.scala-js.org/doc/project/js-environments.html



来源:https://stackoverflow.com/questions/64587429/facing-referenceerror-while-running-tests-with-scalajs-bundler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!