Leah Zorychta
Leah Zorychta

Reputation: 13459

jasmine TypeError: Cannot call method 'expect' of null

When I added the setTimeout I got this error for:

// Suite
describe("sidebar", function() {
    setTimeout(function(){
        document.querySelector('.fa-bars').click();
        expect(document.getElementById('sidebar')!=null).toEqual(true);
    }, 2000);

});

But I don't understand how calling it in a setTimeout can even trigger this error?

Upvotes: 2

Views: 2155

Answers (2)

jcubic
jcubic

Reputation: 66650

You need to call done callback:

describe("sidebar", function() {
    it('should work with setTimeout', function(done) {
        setTimeout(function(){
            document.querySelector('.fa-bars').click();
            expect(document.getElementById('sidebar')!=null).toEqual(true);
            done();
        }, 2000);
    });
});

Upvotes: 1

danba
danba

Reputation: 877

you're using a synchronous test to test asynchronous code. Try

// Suite
describe("sidebar", function() {
    runs( function(){ // encapsulates async code
        setTimeout(function(){
            document.querySelector('.fa-bars').click();
            expect(document.getElementById('sidebar')!=null).toEqual(true);
        }, 2000);
    });

});

For more information, check out https://github.com/pivotal/jasmine/wiki/Asynchronous-specs

Upvotes: 2

Related Questions