0

Consider I have this branch of code:

if (somethingGood) {
  // do something
} else {
  const error = new Error('Some specific message');
  next(error);
}

How can I assert that next is called with that error message (next is stubbed using sinon.stub())?

I initially tried:

expect(next.calledWith('Some specific message')).to.eql(true);

But, of course that does not work because next was actually called with an error.

Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111
  • you haven't told us what testing lib you're using – Joe Warner Jun 15 '18 at 09:42
  • 1
    Is [this question](https://stackoverflow.com/questions/42119260/sinon-calledwithnew-error-and-with-exact-message) helpful? Looks like a duplicate to me, but my vote is binding. ) – raina77ow Jun 15 '18 at 09:42
  • Why does it matter which testing lib I'm using? I don't need a syntactically correct answer, I'm just not sure how to assert against an error object. – Mike Rifgin Jun 15 '18 at 09:44
  • We need to know the assertion library so we can tell you how to assert against an object with that library. At the moment you're just asserting against a string in that example, so I'd imagine you could just do `next.calledWith(new Error('Some specific message'))` but couldn't tell you for sure unless I knew the library. – Elliot Blackburn Jun 15 '18 at 12:28

0 Answers0