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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With