Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular/jasmine: Error: No deferred tasks to be flushed

I'm unit testing one of my directives and I get the following error

Error: No deferred tasks to be flushed

The code I need to test is inside a $timeout

$timeout(function () {
    $window.doStuff();
});

So when I want to check if doStuff was called I do

$timeout.flush();
expect($window.doStuff).toHaveBeenCalled();

However, now I need to test a situation in which I don't expect doStuff to have been called. So I did:

$timeout.flush();
expect($window.doStuff).not.toHaveBeenCalled(); // Notice the not here!!

However, because $timeout was not called, the $timeout.flush() gives me this error.

So the question is, what is the preferred way to test this situation ?

like image 976
Jeanluca Scaljeri Avatar asked Oct 19 '22 16:10

Jeanluca Scaljeri


1 Answers

Use $timeout.verifyNoPendingTasks() to check if no tasks have been added.

Check angular documentation here:

Verifies that there are no pending tasks that need to be flushed.

like image 107
Ilya Ivanov Avatar answered Oct 22 '22 21:10

Ilya Ivanov