Ember testing: you turned on the testing mode, which turned off run-loop autostart

I am trying to write a simple Ember integration test and continue to get a disappointing runtime error despite using Ember.run . I had a nightmare of time, trying to get it to work, if anyone could help me, I would be so grateful. In particular, I can see the test sign and start loading the next page (as it should), but as soon as the test finishes, I get this error. This applies to the second test, the first pass (since nothing is asynchronous, I think).

 import Ember from 'ember'; import startApp from 'jobs-tuftsdaily/tests/helpers/start-app'; import exists from 'jobs-tuftsdaily/tests/helpers/start-app'; var App; module('Integration - Landing Page', { setup: function() { App = startApp(); }, teardown: function() { Ember.run(App, 'destroy'); } }); test('Should load content', function() { visit('/').then(function() { ok(exists("*"), "Found HTML!"); ok(exists('label:eq(4)'), "Slug label on page"); }); }); test('Should sign in test user', function() { Ember.run(function() { visit('/').andThen(function() { return fillIn("input[name=email]", " test@test.com "); }).andThen(function() { return fillIn("input[type=password]", "password"); }).andThen(function() { return click("button"); }).andThen(function() { ok(1, "stupid test passed"); }); }); }); 
+6
source share
3 answers

I realize I'm late for the party, but here anyway:

Somewhere in your component or application code (i.e. NOT in the test code) you are probably listening to some kind of event outside of Ember (like a DOM event, through jQuery or something like that), but try to interact with it at runtime of this handler. These handler functions must be wrapped in Ember.run.bind() , or they will not have runloop during the test. Ember.run.bind() also set this for you during the handler.

If you do not complete event callbacks in Ember.run.bind() , the code will probably run smoothly during normal application startup because the autostart function will find the required runloop (for example, if you plan to use Ember.run.schedule() during the handler) interacts with Ember in the code and runs runloop for them, but it is disabled in tests.

+3
source

Wrapping tests in Ember.run will not help, because the error you receive does not occur, because the test code, but the code of your application. In your sign on the route, you must perform some asynchronous requests (for example, AJAX or setTimeout ), and in your handlers you perform some Ember API actions (for example, configuration, transition, whatever).

If you need any specific help, you need to place your code in the route / controller.

I recently published a book on Run Loop, including an autorun engine, which can be downloaded here .

+1
source

I have the same problem in a component integration test. Error:

The statement failed: you turned on the test mode, which turned off autorun autostart.

You will need to wrap any code with asynchronous side effects in the run.

And I found the cause of the problem for integration tests. I am passing a component with the following code:

 let myobject = Ember.Object.create({x:1}); this.set('param', myobject) this.render(hbs`{{my-object param=param}}`); 

After rendering, updating myobject as shown below causes an error:

 myobject.set('x',2); 

Because it is not inside the ember startup loop.

Instead, an unauthorized call, updating the value should be performed as one of the following:

 this.set('param.x',2); //OR: this.set('param', Ember.Object.create({x:1}); //OR: Ember.run(()=>{ Ember.set(myobject,'x',2); }); 

In your case: I have not tried it, but my opinion is that asynchronous test assistants, such as visit, click, fillIn, can be used in acceptance tests not in integration tests for the following reason.

+1
source

Source: https://habr.com/ru/post/983053/


All Articles