Jasmine: Async callback was not called within the timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

I have an angular service called requestNotificationChannel :

 app.factory("requestNotificationChannel", function($rootScope) { var _DELETE_MESSAGE_ = "_DELETE_MESSAGE_"; function deleteMessage(id, index) { $rootScope.$broadcast(_DELETE_MESSAGE_, { id: id, index: index }); }; return { deleteMessage: deleteMessage }; }); 

I am trying to execute unit test this service using jasmine:

 "use strict"; describe("Request Notification Channel", function() { var requestNotificationChannel, rootScope, scope; beforeEach(function(_requestNotificationChannel_) { module("messageAppModule"); inject(function($injector, _requestNotificationChannel_) { rootScope = $injector.get("$rootScope"); scope = rootScope.$new(); requestNotificationChannel = _requestNotificationChannel_; }) spyOn(rootScope, '$broadcast'); }); it("should broadcast delete message notification", function(done) { requestNotificationChannel.deleteMessage(1, 4); expect(rootScope.$broadcast).toHaveBeenCalledWith("_DELETE_MESSAGE_", { id: 1, index: 4 }); done(); }); }); 

I read about asynchronous support in Jasmine, but since I'm pretty new to unit testing with javascript, I couldn't get it to work.

I get an error message:

 Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL 

and my test is too long to complete (about 5 seconds).

Can someone help me provide a working example of my code with some explanation?

+122
debugging angularjs asynchronous unit-testing jasmine
Mar 24 '14 at 8:48
source share
16 answers

Having an argument in your it function ( done in the code below) will force Jasimne to make an asynchronous call.

 //this block signature will trigger async behavior. it("should work", function(done){ //... }); //this block signature will run synchronously it("should work", function(){ //... }); 

It doesn't matter what the done argument is called, its existence is all that matters. I ran into this problem due to too many copies / pastes.

The Jasmin Asynchronous Support documentation notes that an argument (named done above) is a callback that can be called to tell Jasmine to complete the asynchronous function. If you never call, Jasmine will never know that your test is complete and will eventually stop working.

+209
Aug 12 '14 at 20:02
source share
β€” -

As a workaround, you can increase the timeout limit for evaluating the Jasmine asynchronous callback.

 describe('Helper', function () { var originalTimeout; beforeEach(function() { originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000; }); afterEach(function() { jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; }); it('Template advance', function(doneFn) { $.ajax({ url: 'public/your-end-point.mock.json', dataType: 'json', success: function (data, response) { // Here your expected using data expect(1).toBe(1) doneFn(); }, error: function (data, response) { // Here your expected using data expect(1).toBe(1) doneFn(); } }); }); }); 

Source: http://jasmine.imtqy.com/2.0/introduction.html#section-42

+46
Aug 15 '16 at 23:41
source share

This error can also be caused by skipping inject when initializing the service / factory or something else. For example, this can be thrown by doing this:

 var service; beforeEach(function(_TestService_) { service = _TestService_; }); 

To fix this, simply wrap the function with inject to get the service correctly:

 var service; beforeEach(inject(function(_TestService_) { service = _TestService_; })); 
+20
Nov 25 '15 at 11:10
source share
 import { fakeAsync, ComponentFixture, TestBed } from '@angular/core/testing'; 

use fakeAsync

 beforeEach(fakeAsync (() => { //your code })); describe('Intilalize', () => { it('should have a defined component', fakeAsync(() => { createComponent(); expect(_AddComponent.ngOnInit).toBeDefined(); })); }); 
+8
Feb 02 '18 at 13:34
source share

This error started for me in blue, in a test that always worked. I could not find any suggestions that helped, until I noticed that my Macbook was running sluggishly. I noticed that the processor was bound by another process that I killed. The async Jasmine error has disappeared and my tests are perfect again.

Do not ask me why, I do not know. But in my circumstances, this seemed like a lack of system resources in the fault.

+5
Feb 27 '17 at 16:28
source share

This is more of an observation than an answer, but it can help others who were just as disappointed as I was.

I kept getting this error from two tests in my set. I thought that I just broke the tests with the help of refactoring, which I did, therefore, after the change was returned, it didn’t work, I returned to the earlier code twice (two revisions back), thinking that this would eliminate the error. This has not changed anything. I chased my tail all day yesterday and part of this morning without solving the problem.

I was upset and checked the code on the laptop this morning. I launched the entire test suite (about 180 tests), there are no errors. Thus, errors have never been in code or tests. Returned to my developer box and reloaded it to clear anything in memory that could cause a problem. Unchanged, the same errors on the same two tests. So I deleted the directory from my computer and checked it back. Voila! No mistakes

I don’t know what caused this, or how to fix it, but deleting the working directory and restoring it fixed it, whatever it is.

Hope this helps someone.

+4
Jun 22 '18 at 17:41
source share

You also get this error while expecting something in the beforeAll function!

 describe('...', function () { beforeAll(function () { ... expect(element(by.css('[id="title"]')).isDisplayed()).toBe(true); }); it('should successfully ...', function () { } } 
+3
Nov 30 '17 at 11:11
source share

You can use the karma-jasmine plugin to set the default wait interval worldwide.

Add this config to karma.conf.js

 module.exports = function(config) { config.set({ client: { jasmine: { timeoutInterval: 10000 } } }) } 
+3
Mar 12 '19 at 9:03
source share

In my case, this error was caused by the misuse of "fixture.detectChanges ()". This method seems to be an event listener (asynchronous) that will only respond to a callback when changes are detected. If no changes are found, it will not call a callback, which will result in a timeout error. Hope this helps :)

+2
Jun 14 '18 at 18:43
source share

It works after removing the scope reference and function arguments:

 "use strict"; describe("Request Notification Channel", function() { var requestNotificationChannel, rootScope; beforeEach(function() { module("messageAppModule"); inject(function($injector, _requestNotificationChannel_) { rootScope = $injector.get("$rootScope"); requestNotificationChannel = _requestNotificationChannel_; }) spyOn(rootScope, "$broadcast"); }); it("should broadcast delete message notification with provided params", function() { requestNotificationChannel.deleteMessage(1, 4); expect(rootScope.$broadcast).toHaveBeenCalledWith("_DELETE_MESSAGE_", { id: 1, index: 4} ); }); }); 
+1
Apr 23 '14 at 6:18
source share

If you have the ( done ) argument in the it function, try deleting it and also calling it inside the function itself:

 it("should broadcast delete message notification", function(/*done -> YOU SHOULD REMOVE IT */) { requestNotificationChannel.deleteMessage(1, 4); expect(rootScope.$broadcast).toHaveBeenCalledWith("_DELETE_MESSAGE_", { id: 1, index: 4 }); // done(); -> YOU SHOULD REMOVE IT }); 
0
Oct 11 '16 at 13:23
source share

As @mastablasta notes, but also add that if you call the 'done' argument or rather call it complete, you simply call callback complete () in your test when it is done.

 // this block signature will trigger async behavior. it("should work", function(done){ // do stuff and then call done... done(); }); // this block signature will run synchronously it("should work", function(){ //... }); 
0
Sep 12 '18 at 21:23
source share

jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;

Keeping this in a block solved my problem.

 it('', () => { jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000; }); 
0
Dec 12 '18 at 7:11
source share

do not make long code breaking it into small blocks "it". so it works

0
Jan 21 '19 at 7:36
source share

Try using shorter waiting times. I got this error when using timeout = 5000. I replaced it with 2000 and it worked for me!

0
May 16 '19 at 7:23
source share

What I did: added / updated the following code:

 framework: 'jasmine', jasmineNodeOpts: { // Jasmine default timeout defaultTimeoutInterval: 60000, expectationResultHandler(passed, assertion) { // do something }, } 
0
Jul 02 '19 at 12:33 on
source share



All Articles