Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run


#1

Hello,

While writing acceptance tests for a webapp, I encountered an issue with ember.run.schedule(). Everytime I visit() an URL in acceptance tests and programatically resize the window,to test an onresize handler which performs computation in ember.run.schedule, I get this error message:

"Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run"

I performed a search on the subject and came across this excellent post by Tarasm in this very forum. He explains that the issue comes from calls to run.schedule. However, I tried every single solution he suggested in his post and still got the same error message with all of them, although they were supposed to alleviate the isssue.

Do you have by any chance an idea about the cause of this error message ? The setDynamicStyles method is contains called once on component initalisation, then again on each resize after debunking using setTimeout. I think it may be because I have an Ember.run.schedule() launched in setTimeout in one of my components.

Acceptance test:

test("news", function (assert) {
  assert.expect(1);
  Ember.run(function () {
    visit("/fr/news/notre-nouveau-projet-sur");
  });
  andThen(function() {
    assert.equal(currentURL(), "/fr/news/notre-nouveau-projet-sur");
  });
});

The component which causes the issue:

setDynamicStyles() {
   var _this = this;
  
  _ember["default"].run.schedule("afterRender", function () {  //it seems that this call is causing the error
     var content = _this.$(".carousel-content");
     if (content.outerHeight() < _this.$().height()) {
       content.addClass("vcentered");
       _this.$().addClass("noscroll");
     } else {
        content.removeClass("vcentered");
        _this.$().removeClass("noscroll");
     }
  });
},
didRender() {                    //the setDynamicStyle method which causes the issue is called once after component redering
  this.setDynamicStyles();
  this.$("img").load(this.setDynamicStyles.bind(this));
},

#2

Did you want solution for this?. Currently I stuck with this few day already


#3

The very newest versions of Ember no longer have this assertion, so the problem is just automatically fixed for you when you can upgrade.

But the solution is that any callback that will be called from non-Ember code should get wrapped in Ember.run.

In the case of the original post above, the problem is that this.$("img").load() is going to invoke this.setDynamicStyles from outside Ember at some arbitrary time. So it needs to create a runloop.

One solution is to change

  this.$("img").load(this.setDynamicStyles.bind(this));

to

  this.$("img").load(() => Ember.run(() => this.setDynamicStyles()))