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 test
// ... initialize angular module, inject $rootScope, $timeout, $httpBackend and service
it('tests async functionality in standard way', function(done) {
AsyncService.resolveAsync()
.then(function(response) {
expect(response).toBe('response');
// call done() explicitly
done();
});
// we use $timeout in service, which is mocked because we included angular-mocks so we have to trigger manually
$timeout.flush();
// we have to call $rootScope.$digest() to process pending promises in angular context
$rootScope.$digest();
// we have to call $httpBackend.flush to process pending requests
$httpBackend.flush()
//Be aware, that the upper three functions must be callen in the correct order,
//depending on the implementation of the code under test
});
// vs
// jasmine-async-sugar
itAsync('tests async functionality without "done", manual "$rootScope.$digest" and "$timeout.flush" triggering', function() {
// only thing we need to do is return promise
// $rootScope.$digest(), $timeout.flush() and done() are handled automatically by library
return AsyncService.resolveAsync()
.then(function(response) {
expect(response).toBe('response');
});
});
itAsync
fitAsync
xitAsync
beforeEachAsync
beforeAllAsync
afterEachAsync
afterAllAsync
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
module.exports = function(config) {
config.set({
// ...
files: [
// standard angular testing setup
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
// our drop in library file
'bower_components/jasmine-async-sugar/jasmine-async-sugar.js',
// test app and spec files
'test/**/*.js',
'test/**/*.spec.js'
]
// ...
});
};
Check example application and corresponding tests.
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.
Please, feel free to submit bugs (prefferably with pull requests) or new features.
- 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