pietvanzoen / jasmine-promise-matchers

Adds toResolve and toReject matchers to jasmine.
MIT License
1 stars 0 forks source link

Promise Matchers

CircleCI branch

Adds toResolve and toReject matchers to jasmine.

Installation

$ npm install --save-dev @pietvanzoen/jasmine-promise-matchers

Usage

Install the promise matchers using the install method.

var promiseMatchers = require('@pietvanzoen/jasmine-promise-matchers');

beforeEach(function() {
  promiseMatchers.install(jasmine);
});

When using a non-native implementation of promises you can pass your Promise constructor as an option.

var promiseMatchers = require('@pietvanzoen/jasmine-promise-matchers');
var RSVP = require('rsvp');

beforeEach(function() {
  promiseMatchers.install({
    jasmine: jasmine,
    Promise: RSVP.Promise
  });
});

toResolve

it('passes when promise resolves', function(done) {
  expect(Promise.resolve()).toResolve(done); // PASS
});
it('fails when promise rejects', function(done) {
  expect(Promise.reject()).toResolve(done); // FAIL
});

toReject

it('passes when promise rejects', function(done) {
  expect(Promise.reject()).toReject(done); // PASS
});
it('fails when promise resolves', function(done) {
  expect(Promise.resolve()).toReject(done); // FAIL
});

callbacks

it('passes promise response to callback', function(done) {
  expect(Promise.resolve('foo bar')).toResolve(done, function(resp) {
    expect(resp).toBe('foo bar'); // PASS
  });
});

callback returning a promise

it('callback can return a promise', function(done) {
  expect(Promise.resolve('foo bar')).toResolve(done, function(resp) {
    return Promise.resolve()
      .then(function() {
        // `done` is not called until this promise chain completes
      });
  })
});
it('fails when callback promise rejects', function(done) {
  expect(Promise.resolve('foo bar')).toResolve(done, function(resp) {
    return Promise.resolve()
      .then(function() {
        throw new Error('foo');
      }); // FAILS and reports Error
  })
});

TODO

Contributing

Don't be shy! Submit issues (or better yet PRs) if you see anything that could be better. If you're submitting code that contains patches or features please try to include unit tests. Thanks!

Author

Piet van Zoen hi@pietvanzoen.com

License

MIT : http://opensource.org/licenses/MIT