0

I would like to know how to stop the console from printing stack dumps on test with promises that throw errors.

doCallTest = function() {
  var p = new Promise(function(resolve, reject) {
      reject(new Error('Nooooo'));
  }).catch(function(err) {
    throw new Error(err);
  });
  return Promise.delay(100).return(p);
};


// This prints error message `Error: Nooooo` and stack dump and halts the test
return expect(doCallTest()).to.eventually.be.rejectedWith('Nooooo');

I see a lot of mentions of similar sounding issues but not sure how to resolve it. There is Bluebird.onPossiblyUnhandledRejection but that is just messy:

var P = require('bluebird');
P.onPossiblyUnhandledRejection(function(error){
   expect(error.message).to.be.eql('Nooooo');
   done();
});
doCallTest();

How can I make use of Chai's thrown error tests with promises?

Edit I've narrowed it down to the delay() call. Removing that makes it work fine.

Perhaps related: https://github.com/petkaantonov/bluebird/issues/100

Update The issue seems to have been because of my use of delay() as:

return Promise.delay(100).return(p);

Instead of:

return p.delay(100);

The latter had not worked for me initially which is why I changed it - I suspect it was becasue of how my catch calls were setup. By changing things around I was able to use the latter format:

doCallTest = function() {
  var p = new Promise(function(resolve, reject) {
      reject(new Error('Nooooo'));
  });
  return p.delay(100);
};
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • I think I might have asked you this before (or was it someone else) - why aren't you using the promise syntax in mocha? You can simply return promises instead of using that `done` nonsense. – Benjamin Gruenbaum Feb 21 '15 at 11:31
  • @BenjaminGruenbaum just for display. It's the same result. – cyberwombat Feb 21 '15 at 13:54
  • @BenjaminGruenbaum - edited question - I see that you posted a lot about something that seems related. Perhaps this makes it clearer. – cyberwombat Feb 21 '15 at 15:10

1 Answers1

1

doCallTest returns a promise that rejects with an error, not the string "Nooooo". (as a side note, please don't reject with non-errors)

Try this, it will reject with the string:

doCallTest = function() {
  return P.reject('Nooooo');
};
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • I need to eventually throw an error from this string ( I have a catch handler in the code so that everything trickles down nicely). I can't seem to throw the error inside of P() as it doesnt cascade so I reject the error message and immediately catch/throw it. – cyberwombat Feb 21 '15 at 17:47
  • @Yashua that doesn't make any sense, if you need to create a rejected promise with a certain error, simply do `Promise.reject(theError)`, anything else is completely unnecessary. – Esailija Feb 21 '15 at 17:49
  • As in `return P.reject(new Error('Nooooo'));`? That doesn't change any of the issues I have. – cyberwombat Feb 21 '15 at 17:55
  • ok just reproduce your issue in this jsfiddle (it works fine) http://jsfiddle.net/2x4n2dd9/1/ – Esailija Feb 21 '15 at 17:56
  • That's a string in the fiddle - not an Error. I don't have issues with strings - just error stack dumps – cyberwombat Feb 21 '15 at 17:58
  • Indeed :) - Seems that `rejectedWith` must be used and not `throw` as in regular chai. Thank you for your time. I will give it a shot in the real code – cyberwombat Feb 21 '15 at 18:07
  • I've applied it to my code and after still experiencing the issue realized it's because of a `delay()` call. Would you mind reviewing the edited code I put up please? – cyberwombat Feb 21 '15 at 21:12