Adds toResolve
and toReject
matchers to jasmine.
$ npm install --save-dev @pietvanzoen/jasmine-promise-matchers
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
});
});
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
});
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
});
it('passes promise response to callback', function(done) {
expect(Promise.resolve('foo bar')).toResolve(done, function(resp) {
expect(resp).toBe('foo bar'); // PASS
});
});
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
})
});
Promise
library.addMatchers
function to installDon'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!
Piet van Zoen hi@pietvanzoen.com