简体   繁体   中英

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'
    ],

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.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM