Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine and Karma: Cannot find variable runs

Do I need a plugin/lib to use runs() and waits() with Jasmine? I'm looking at the Jasmine wiki page for async tests: https://github.com/pivotal/jasmine/wiki/Asynchronous-specs.

They have no mention of needing a special lib/plugin, so I assume runs() and waits() should work out of the box.

My code looks like this (it's wrapped in a describe):

it('test', function() {
    runs(function() {

    });
});

I'm getting: ReferenceError: runs is not defined

The relevant portion of my karma config is:

    files: [
        'bower_components/jquery/dist/jquery.min.js',
        'bower_components/angular/angular.js',
        'bower_components/angular-mocks/angular-mocks.js',
        'src/*.js',
        'test/*.spec.js'
    ],


    frameworks: ['jasmine'],

    browsers: ['PhantomJS'],

    plugins: [
        'karma-spec-reporter',
        'karma-chrome-launcher',
        'karma-firefox-launcher',
        'karma-jasmine',
        'karma-phantomjs-launcher'
    ],
like image 394
user2736286 Avatar asked Mar 21 '26 17:03

user2736286


2 Answers

Ok, so it turns out Jasmine 2.0 has removed runs(), waits() and waitsFor(). The new async support uses done(), which be found at: http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support. I've added a quick note to the outdated github wiki page about this.

like image 123
user2736286 Avatar answered Mar 24 '26 06:03

user2736286


Basically if you want to run an async test, you should use the $httpBackend service, which when flushed will trigger the right events and your tests will run smoothly.

EDIT: Each of the angularjs timed actions are wrapped in promises and digest cycles. In the case of $timeout to trigger the digest cycle that triggers it you can do $timeout.flush().

For example you can have:

expect($scope.timedvariable).toEqual('before timeout value');
// Flush the timeout service
$timeout.flush();
// the actions within the $timeout are executed
expect($scope.timedvariable).toEqual('value after timeout');
like image 26
Mimo Avatar answered Mar 24 '26 05:03

Mimo