Search code examples
scalascala.jsscalajs-bundler

Facing ReferenceError while running tests with ScalaJS Bundler


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

Solution

  • 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