Search code examples
javascriptember.jssinonacceptance-testing

Is there a way to run Ember.Testing acceptance tests with fake timers?


I'm working on a project in EmberJS that has a set of acceptance tests (qUnit with EmberJS helpers). Now, I'm trying to optimize those tests as much as possible because waiting 10 minutes for every run is just not nice.

Some tests that we have actually need to wait for some things to happen: a timer to run out, a minute to pass for the clocks to update, etc.

I tried using sinonjs with faketimers, but this seems to mess up the ember run loop and any other usage of setInterval / setTimeout that is related to the application or tests.

I'm looking for a way to integrate the tests with such functionality. Options I would consider are:

  • making patches into Ember / Ember.Testing or Sinon
  • using another faketimer approach (besides sinon)
  • manually faking into the Date object to work on some kind of non-intrusive fake timer

Now, I guess that somebody must have face this already, so before I jump to any conclusions I needed to ask: is there a recommended approach to this situation?

Thanks!


Solution

  • Our particular solution to this problem was to pull the constant values out of the controllers/routes and put it into the App's namespace, then change it during testing.

    App = Em.Application.create({
      someTimerLength:2000
    });
    
    // if testing mode, change a ton of things 
    // you could also use a different config hash when creating the app
    if(Ember.testing){
      Em.set(App,'someTimerLength', 1);  
    }
    

    Random code referencing the app namespace

      Ember.run.later(function(){
        resolve(['red', 'yellow', 'blue']);
      }, App.someTimerLength);
    

    Here's a ugly example, but shows how we do it: http://emberjs.jsbin.com/wipo/41/edit