promise-matchers

Custom jasmine matchers for testing asynchronous javascript

promise-matchers

This library introduces a simple set of custom Jasmine matchers for use with asynchronous JavaScript. It can be used with any promises library that implements the Promises/A+ specification, such as Q, Promise, RSVP, WinJS or jQuery.

expect(promise).toHaveBeenResolved(done);
expect(promise).toHaveBeenResolvedWith(done, expectation);
expect(promise).toHaveBeenRejected(done);
expect(promise).toHaveBeenRejectedWith(done, expectation);

Either you use in your browser jasmine test runner by adding it after the script-tag jasmine.js:

<script src="promise-matchers/src/promise-matchers.js"></script>

Or when using jasmine-node you can simply install the matchers via:

npm install promise-matchers --save -dev

And make them available in your spec-file:

require('promise-matchers');
describe(...);
 
// or via requirejs (assuming your specs are within PROJECT_ROOT/test): 
require([
  '../node_modules/promise-matchers/src/promise-matchers.js',
], function() {
  describe(...);
});
it('succeeds', function(done) {
    var promise = foo();
    expect(promise).toHaveBeenResolved(done);
});
it('fails', function(done) {
    var promise = foo();
    expect(promise).toHaveBeenRejected(done);
});
it('succeeds with value of 3', function(done) {
    var promise = foo();
    expect(promise).toHaveBeenResolvedWith(done, function(result) {
        expect(result).toBe(3);
    });
});
it('fails without saving', function(done) {
    var save = spyOn(Bar, 'save');
    var promise = foo();
    expect(promise).toHaveBeenRejectedWith(done, function() {
        expect(save).not.toHaveBeenCalled();
    });
});
  1. If a promise with an expectation of toHaveBeenResolved or toHaveBeenResolvedWith is rejected, the matcher fails with the message Expected promise to have been resolved.
  2. If a promise with an expectation of toHaveBeenRejected or toHaveBeenRejectedWith is resolved, the matcher fails with the message Expected promise to have been rejected.
  3. If an expectation passed to toHaveBeenResolvedWith or toHaveBeenRejectedWith fails, the matcher fails with that expectation's failure message.
  4. If an expectation passed to toHaveBeenResolvedWith or toHaveBeenRejectedWith throws an exception, the matcher fails with the exception text.
  5. These matchers will not behave properly with not.