Skip to content

Commit

Permalink
make sure catches catch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stewart authored and a-b-r-o-w-n committed Apr 13, 2016
1 parent 3d87d0d commit 9a0d8fd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ function buildThenable() {
}

if (this.rejected && onReject) {
onReject(this.rejectValue);
return this;
return this.catch(onReject);
}
return this;
},

catch: function(onReject) {
if (this.rejected) {
try {
onReject(this.rejectValue);
const value = onReject(this.rejectValue);
this.resolved = true;
this.rejected = false;
this.resolveValue = value;
this.rejectValue = undefined;
} catch (e) {
this.rejectValue = e;
}
Expand Down
53 changes: 49 additions & 4 deletions spec/index_spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var sinon = require('sinon');
var sinonStubPromise = require('../index');
var AssertionError = require('chai').AssertionError;
var expect = require('chai').expect;
var chai = require('chai');
var RSVP = require('rsvp');
var expect = chai.expect;
var assert = chai.assert;

sinonStubPromise(sinon);

Expand All @@ -29,16 +31,16 @@ describe('stubPromise', function() {

expect(resolveValue).to.equal('resolve value');
});

it('can resolve multiple times with the same value', function() {
var secondResolvedValue = null;
promise.resolves('resolve value');

promise().then(function(arg) {
resolveValue = arg;
});
expect(resolveValue).to.equal('resolve value');

promise().then(function(arg) {
secondResolvedValue = arg;
});
Expand Down Expand Up @@ -186,6 +188,32 @@ describe('stubPromise', function() {
expect(finallyCalled).to.be.true;
});

it('handles catches that succeed', function(done) {
promise.rejects('an error');

promise().then(f, function(e) {
expect(e).to.equal('an error');
return 'no error';
}).then(function (d) {
expect(d).to.equal('no error');
done();
})
});

it('handles catches that fail', function(done) {
promise.rejects('an error');

promise().then(f, function(e) {
expect(e).to.equal('an error');
throw new Error('another');
}).then(function () {
done(new Error("Promise did not throw"))
}, function (e) {
expect(e.message).to.equal('another');
done();
})
});

describe('chaining', function() {
it('supports then chaining', function(done) {
promise().then(f).then(f);
Expand Down Expand Up @@ -297,5 +325,22 @@ describe('stubPromise', function() {
done();
});
});

it('does execute additional then blocks when an error has been caught', function(done) {
promise.resolves();

promise()
.then(function() {
throw new Error('Stop the insanity');
})
.catch(function(error) {
expect(error.message).to.eql('Stop the insanity');
return 'No more error';
})
.then(function (r) {
expect(r).to.eql('No more error');
done();
});
});
});
});

0 comments on commit 9a0d8fd

Please sign in to comment.