I have a class that rejects a promise:
Sync.prototype.doCall = function(verb, method, data) {
var self = this;
self.client = P.promisifyAll(new Client());
var res = this.queue.then(function() {
return self.client.callAsync(verb, method, data)
.then(function(res) {
return;
})
.catch(function(err) {
// This is what gets called in my test
return P.reject('Boo');
});
});
this.queue = res.delay(this.options.throttle * 1000);
return res;
};
Sync.prototype.sendNote = function(data) {
var self = this;
return self.doCall('POST', '/Invoice', {
Invoice: data
}).then(function(res) {
return data;
});
};
In my test:
return expect(s.sendNote(data)).to.eventually.be.rejectedWith('Boo');
However while the test passes it throws the error to the console.
Unhandled rejection Error: Boo ...
With non promise errors I have used bind to test to prevent the error from being thrown until Chai could wrap and test:
return expect(s.sendNote.bind(s, data)).to.eventually.be.rejectedWith('Boo');
However this does not work with this and returns:
TypeError: [Function] is not a thenable.
What is the correct way to test for this?
You can chain promises with await if you choose to. function generateException() { return new Promise(reject => { return reject(new Error('Promise Rejected'); }) it('should give an error', async ()=> { await generateException(). catch(error => { expect(error. message).to.
If the Promise rejects, the second function in your first . then() will get called with the rejected value, and whatever value it returns will become a new resolved Promise which passes into the first function of your second then.
(Disclaimer: This is a good question even for people that do not use Bluebird. I've posted a similar answer here; this answer will work for people that aren't using Bluebird.)
Here's how you can use chai-as-promised to test both resolve
and reject
cases for a Promise:
var chai = require('chai');
var expect = chai.expect;
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
...
it('resolves as promised', function() {
return expect(Promise.resolve('woof')).to.eventually.equal('woof');
});
it('rejects as promised', function() {
return expect(Promise.reject('caw')).to.be.rejectedWith('caw');
});
You can accomplish the same without chai-as-promised like this:
it('resolves as promised', function() {
return Promise.resolve("woof")
.then(function(m) { expect(m).to.equal('woof'); })
.catch(function(e) { throw e }) // use error thrown by test suite
;
});
it('rejects as promised', function() {
return Promise.reject("caw")
.then(function(m) { throw new Error('was not supposed to succeed'); })
.catch(function(m) { expect(m).to.equal('caw'); })
;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With