rstacruz/jsdom-global

Travis unit tests failing with 'window is not defined'

Opened this issue · 0 comments

Hi there,

In our unit tests we use the following construct to initialize jsdom-global:

before(function() {
    this.jsdom_global = jsdom_global(
      "<div id='mynetwork'></div>",
      { skipWindowCheck: true}
    );
    this.container = document.getElementById('mynetwork');
  });

  after(function() {
    this.jsdom_global();
  });

The unit tests work perfectly when run locally, but it happens regularly that they fail on Travis:

1) Network "after all" hook:
     Uncaught ReferenceError: window is not defined
      at CanvasRenderer._requestNextFrame (dist/vis.js:50457:22)
      at CanvasRenderer._requestRedraw (dist/vis.js:50540:14)
      at Network.Emitter.emit (dist/vis.js:6493:20)
      at View.animateView (dist/vis.js:51596:29)
      at View.moveTo (dist/vis.js:51536:12)
      at View.fit (dist/vis.js:51462:12)
      at Network.Emitter.emit (dist/vis.js:6493:20)
      at PhysicsEngine._finalizeStabilization (dist/vis.js:47781:27)
      at PhysicsEngine._stabilizationBatch (dist/vis.js:47767:14)

After this happens, other unrelated unit tests tend to fail as well as a consequence.
I don't fully understand the mechanics of execution on Travis, but by the looks of it, the global window is cleared while (apparently) overlapping unit tests are running.

Is there any way to avoid this error?

As a stopgap measure, I'm considering just catching the ReferenceError for this particular case; I see in the code that window is the very last item in the list of keys, so everything else would have been cleaned up on the call to jsdom_global() in the after-hook. Would this be acceptable? If you know of any other solutions, I would love to know.


This is my current working hypothesis on this error:

In our code, window.requestAnimationFrame is used to trigger the drawing of a next frame on a Canvas element. The final frame redraw can be delayed, and it's possible that this overlaps with the subsequent unit test (on Travis). Because of this, the global window could be reset while another test is running, causing the given error.