How to test a function which has a setTimeout with jasmine?

后端 未结 4 1562
失恋的感觉
失恋的感觉 2020-12-02 22:00

I need to write a test for a function that has a setTimeout() call inside, but i can\'t find how i should do.

This is the function

// Di         


        
4条回答
  •  时光取名叫无心
    2020-12-02 22:41

    Since Jasmine 2 the syntax has changed: http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support

    You now can simply pass a done callback to beforeEach, it, and afterEach:

    it('tests something async', function(done) {
        setTimeout(function() {
            expect(somethingSlow).toBe(true);
            done();
        }, 400);
    });
    

    Update: Since writing this it's now also possible to use async/await which would be my preferred approach.

提交回复
热议问题