promise-matchers

0.9.6 • Public • Published

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.

Matchers

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

Usage

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(...);
});

Examples

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();
    });
});

Notes

  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 or with the message Expected promise to have been resolved but it was rejected with: [possible promise reject message].
  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 but it was resolved with: [possible promise resolve value].
  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.

Package Sidebar

Install

npm i promise-matchers

Weekly Downloads

21

Version

0.9.6

License

Apache 2.0

Last publish

Collaborators

  • d215steinberg