jasmine-async-sugar
Simple drop-in syntax sugar for Jasmine 2.X
test framework to enhance testing of async (promise) functionality in Angular 1.X
applications.
Library adds extra global methods which handle async tests implicitly without need to call $rootScope.$digest();
, $timeout.flush();
,$httpBackend.flush();
, or done();
manually. Only thing you need to do is to return the promise in your test function. This approach was inspired by Mocha
test framework which waits for resolution of returned promises by default before progressing to next test block.
Standard Jasmine 2.X test vs jasmine-async-sugar
// standard test // ... initialize angular module, inject $rootScope, $timeout, $httpBackend and service ; // vs // jasmine-async-sugar ;
Supported methods
itAsync
fitAsync
xitAsync
beforeEachAsync
beforeAllAsync
afterEachAsync
afterAllAsync
How to use it?
bower install jasmine-async-sugar --save-dev
orbower i jasmine-async-sugar -D
- add
jasmine-async-sugar.js
reference tofiles
array inkarma.conf.js
or in karma task ofgrunt
(gulp
or other build system...) - adjust your tests to use async methods
Example karma.conf.js
module { config;};
Example of tests using async methods
Check example application and corresponding tests.
Motivation
Library was created because we encountered problem using standard $rootScope.$digest();
at the end of the test in one particular situation where we were chaining Angular's $q
and Node's q
promises together. In that case one call to $rootScope.$digest();
isn't enough even if all q
promises are properly wrapped with $q.when(qPromise);
. Library internaly uses setInterval
, which will call $rootScope.$digest();
until all chained promises are resolved and done();
called. At the end the inteval is cleared.
Contributing
Please, feel free to submit bugs (prefferably with pull requests) or new features.
How to run tests?
- clone repository
git clone https://github.com/tomastrajan/jasmine-async-sugar.git
- install dependencies by running
npm install
andbower install
- run tests by running
npm test