I love using console log for feedback perhaps too much, and sometimes I run into code that as convention we've added $timeout in the directive/service/controller, sometimes as long as 500 ms, and now the problem is during unit test, I noticed only console.logs directly under the it constructor gets sent out to karma and output to the screen.
wrapped console logs under timeout or rather wrapped assertions under $timeout do not yield any result as if ignored, what's the solution to timeouts?
Karma is a direct product of the AngularJS team from struggling to test their own framework features with existing tools. As a result of this, they made Karma and have transitioned it to Angular as the default test runner for applications created with the Angular CLI.
useFakeTimers() . This is replacing the original implementation of setTimeout() and other timer functions. Timers can be restored to their normal behavior with jest.
it('expects testableVariable to become true', function(done) { testableCode(); setTimeout(function() { expect(testableVariable). toEqual(true); done(); }, 20); }); Additionally, the timer behavior could be mocked ... this method allows jasmine to step the time forward.
In your unit tests, you load ngMock
, which overwrites the orignal $timeout
with its mock. Mock $timeout
doesn't work like the real JavaScript timeout
. To get it to call the code that's inside it, you have to do $timeout.flush()
from your unit test.
If $timeout
worked like the real timeout
, you would've had to write asynchronous unit-tests for all functions that use $timeout
.
Here's an example of a simplified function that uses $timeout
and how I test it:
gaApi.getReport = function() { report = $q.defer() $timeout(function() { $http({method: 'GET', url: 'https://www.googleapis.com/analytics/v3/data/ga'}) .success(function(body) { report.resolve(body) }) }, 300) return report.promise }
A unit test:
describe('getReport', function() { it('should return report data from Google Analytics', function() { gaApi.getReport().then(function(body) { expect(body.kind).toBe('analytics#gaData') }) $timeout.flush() $httpBackend.flush() }) })
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