Some how chai its not working
promise = doSomething()
expect(promise).to.eventually.equal(obj)
expect(promise).to.eventually.deep.equal(obj)
but in the assertion error I get this
AssertionError: expected {a: "2", b: "3"} to equal {a: "2", b: "3"}
BTW I already try with should
and I get the same result
Some how chai its not working
promise = doSomething()
expect(promise).to.eventually.equal(obj)
expect(promise).to.eventually.deep.equal(obj)
but in the assertion error I get this
AssertionError: expected {a: "2", b: "3"} to equal {a: "2", b: "3"}
BTW I already try with should
and I get the same result
-
It's working for me with a simple
return expect(Promise.resolve({ a: "2", b: "3" })).to.eventually.deep.equal({ a: "2", b: "3" });
. Are you sure the result truly matches the expected output? – Jacob Commented Jan 19, 2016 at 0:50 -
Are you sure you're not running both assertions? Using the
deep
flag is correct, since you shouldn't expect object equality. – Jacob Commented Jan 19, 2016 at 0:54 -
No, Im not running both at the same time. @Jacob yes your solution works for me too using
when
but no thru my promise, the weird part is that the assertion error is getting the same values. – Charlires Commented Jan 19, 2016 at 1:14 -
What plugins are you using?
chai-as-promised
is what I use. – Jacob Commented Jan 19, 2016 at 1:26 -
1
I guess maybe the value return from promise is string of
JSON
, like"{a: '2', b: '3'}"
, if so, tryJSON.parse()
this value before deep equal... – zangw Commented Jan 19, 2016 at 1:29
3 Answers
Reset to default 3I guess maybe the value return from promise is string of JSON, like
"{a: '2', b: '3'}"
Which case I met before. If so, try JSON.parse()
this value before deep equal operation.
Seems like chai checks more than the object structure.
I did this inside the promise JSON.parse(JSON.stringify(response))
and that make it work, so according to this question How to copy JavaScript object to new variable NOT by reference? I assume that chai is checking the reference of the objects, which makes no sense to me at all.
Not the solution I was expecting but works for me, is some one has a better solution please share.
Object.getOwnPropertyNames(obj)
is a good way to look at what the object actually contains, in the case that they are both objects, look the same even in a console log but are not the same.
For example, chai deep.equal
will recognize {a:1, b:2}
and the similar object from a Mongoose instance as not the same but it wont actually tell you what is different ("+ expected - actual" being empty). Object.getOwnPropertNames
will show the real contents of the object.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742311443a4419975.html
评论列表(0条)