Hi I have a feature complete web-app written using AngularJS (1.5.11) and now I'm getting started with unit testing using karma (0.12.37), grunt-karma (0.8.3), karma-chrome-launcher (0.1.12), karma-phantomjs-launcher (1.0.4), phantomjs-prebuilt (2.1.14), jasmine-promise-matchers (2.3.0) and karma-jasmine (0.1.6), with a 1.3.x Jasmine version.
I'm not very confident in testing asynchronous stuff, so I started googling around and I always end up seeing the only mandatory thing to run AngularJS async tests is a $rootScope/$scope.$apply/$digest
right after the async function has been called.
Eventually I found someone suggesting me to use runs() and waitsFor() and the test in this plunkr in particular runs smoothly when using the chrome-launcher but fails when using phantomjs-launcher, throwing an error like the following:
Expected { myError : { error : 'error_message' }, line : <factory's line of code which throws the error>, sourceURL : 'path/to/factory.js', stack :
<function throwing error> B@path/to/factory.js:<factory's line of code which throws the error>
<"async" function> A@path/to/factory.js:<factory's line of code which calls B()>
path/to/factory-spec.js:<the following line of code: var promise = HandleService.A();>
invoke@path/to/angular/angular.js:4771:24
WorkFn@path/to/angular-mocks/angular-mocks.js:3130:26
execute@path/to/node_modules/karma-jasmine/lib/jasmine.js:1145:22
next_@path/to/node_modules/karma-jasmine/lib/jasmine.js:2177:38
start@path/to/node_modules/karma-jasmine/lib/jasmine.js:2130:13
execute@path/to/node_modules/karma-jasmine/lib/jasmine.js:2458:19
next_@path/to/node_modules/karma-jasmine/lib/jasmine.js:2177:38
start@path/to/node_modules/karma-jasmine/lib/jasmine.js:2130:13
execute@path/to/node_modules/karma-jasmine/lib/jasmine.js:2604:19
next_@path/to/node_modules/karma-jasmine/lib/jasmine.js:2177:38
start@path/to/node_modules/karma-jasmine/lib/jasmine.js:2130:13
execute@path/to/node_modules/karma-jasmine/lib/jasmine.js:2604:19
next_@path/to/node_modules/karma-jasmine/lib/jasmine.js:2177:38
onComplete@path/to/node_modules/karma-jasmine/lib/jasmine.js:2173:23
finish@path/to/node_modules/karma-jasmine/lib/jasmine.js:2561:15
path/to/node_modules/karma-jasmine/lib/jasmine.js:2605:16
next_@path/to/node_modules/karma-jasmine/lib/jasmine.js:2187:24
onComplete@path/to/node_modules/karma-jasmine/lib/jasmine.js:2173:23
finish@path/to/node_modules/karma-jasmine/lib/jasmine.js:2561:15
path/to/node_modules/karma-jasmine/lib/jasmine.js:2605:16
next_@path/to/node_modules/karma-jasmine/lib/jasmine.js:2187:24
onComplete@path/to/node_modules/karma-jasmine/lib/jasmine.js:2173:23
finish@path/to/node_modules/karma-jasmine/lib/jasmine.js:2432:15
path/to/node_modules/karma-jasmine/lib/jasmine.js:2459:16
next_@path/to/node_modules/karma-jasmine/lib/jasmine.js:2187:24
path/to/node_modules/karma-jasmine/lib/jasmine.js:2167:23' }, pending : undefined, processScheduled : false } } to be rejected with { myError : { error : 'error_message' } }.
So I started to think Chrome results were false positive, and I needed to rewrite the async tests: hence I tried with something like this plunkr, but now the tests fail both in PhantomJs and Chrome with the expected timeout message:
timeout: timed out after 1500 msec waiting for A should catch an error
N.B.: I can't update Jasmine to version 2.0 and use the done
parameter mechanism now and if I got it right I should not even manually trigger a $rootScope.$apply/$digest
when using jasmine-promise-matchers.