Skip to content

Commit

Permalink
Fixed issue that prevented save() from being aborted when Promise.rej…
Browse files Browse the repository at this point in the history
…ect was returned in a hook. Fixes #57
  • Loading branch information
scottwrobinson committed Feb 29, 2016
1 parent b2bd984 commit 9e99ee0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/base-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ class BaseDocument {

var hookPromises = [];
hookPromises = hookPromises.concat(_.invoke(embeddeds, hookName));
hookPromises.push(_.invoke([this], hookName));
hookPromises.push(this[hookName]());
return hookPromises;
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/issues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,34 @@ describe('Issues', function() {
}).then(done, done);
});
});

describe('#57', function() {
it('should not save due to Promise.reject in hook', function(done) {
/*
* Rejecting a Promise inside of a pre-save hook should
* cause the save to be aborted, and the .caught() method
* should be invoked on the Promise chain. This wasn't
* happening due to how the hooks were being collected
* and executed.
*/

class Foo extends Document {
constructor() {
super();

this.bar = String;
}

preValidate() {
return Promise.reject('DO NOT SAVE');
}
}

Foo.create({bar: 'bar'}).save().then(function(foo) {
expect.fail(null, Error, 'Expected error, but got none.');
}).catch(function(error) {
expect(error).to.be.equal('DO NOT SAVE');
}).then(done, done);
});
});
});

0 comments on commit 9e99ee0

Please sign in to comment.