1

I have a function that inserts into a table and returns a Promise. I am testing with chai-as-promised that duplicate inserts are rejected. What I want to test is the rejection, and the reason code. When I run the function and log console.log(err.reason.code) I get ER_DUP_ENTRY. Confirming this is sufficient for my test so I try to assert it as follows:

expect(insertTable()).to.eventually.be.rejected.and.eventually.to.have.deep.property('reason',{code: 'ER_DUP_ENTRY'}).notify(done);

The syntax seems to be correct, according to chaijs. But I get a failed test:

AssertionError: expected { Object (status, reason) } to have deep property 'reason' of { code: 'ER_DUP_ENTRY' }, but got [Error: ER_DUP_ENTRY: Duplicate entry 'my_dupe' for key 'name_UNIQUE']

This seems contradictory of what my console log says the property is.

Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
  • could you show the code where the promise is rejected? I understand that the promise is rejected with an `Error` object and you wait `Object` with `reason` property – Troopers Mar 29 '18 at 06:45

1 Answers1

0

In a rejection case, reason is the object evaluated by chai-as-promised. So your assertion must be :

return expect(insertTable()).to.eventually.be.rejected.and.have.property("code").equal('ER_DUP_ENTRY');

Don't forget to return the promise to wait for the fulfillment of the promise chain

Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
Troopers
  • 5,127
  • 1
  • 37
  • 64
  • `reason` is a property I return the error back into. I run into `AssertionError: expected { Object (status, reason) } to have property 'code'` when I try to execute your example. – Jordan.J.D Mar 28 '18 at 15:09