0

New to Jasmine, I am testing one async function. Its showing a error saying Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. Please help if I am missing something here.

Function to test :

function AdressBook(){
   this.contacts = [];
   this.initialComplete = false;
}
AdressBook.prototype.initialContact = function(name){
   var self = this;
   fetch('ex.json').then(function(){
       self.initialComplete = true;
       console.log('do something');
   });
}

Testing specs are as below :

var addressBook = new AdressBook();
     beforeEach(function(done){
         addressBook.initialContact(function(){
             done();
         });
     });
     it('should get the init contacts',function(done){
          expect(addressBook.initialComplete).toBe(true);
          done();
     });

1 Answer 1

0

Source

function AddressBook() {
    this.contacts = [];
    this.initialComplete = false;
}

AddressBook.prototype.initialContact = function(name) {
    var self = this;
    // 1) return the promise from fetch to the caller
    return fetch('ex.json').then(function() {
            self.initialComplete = true;
            console.log('do something');
        }
        /*, function (err) {
              ...remember to handle cases when the request fails 
           }*/
    );
}

Tests

describe('AddressBook', function() {
    var addressBook = new AddressBook();
    beforeEach(function(done) {
        // 2) use .then() to wait for the promise to resolve
        addressBook.initialContact().then(function() {
            done();
        });
    });
    // done() is not needed in your it()
    it('should get the init contacts', function() {
        expect(addressBook.initialComplete).toBe(true);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.