javascript - chai deep equal and equal not working - Stack Overflow

Some how chai its not workingpromise = doSomething()expect(promise).to.eventually.equal(obj)expect(prom

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

Share Improve this question edited Jan 19, 2016 at 0:53 Jacob 78.9k24 gold badges156 silver badges241 bronze badges asked Jan 19, 2016 at 0:42 CharliresCharlires 8731 gold badge11 silver badges30 bronze badges 9
  • 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, try JSON.parse() this value before deep equal... – zangw Commented Jan 19, 2016 at 1:29
 |  Show 4 more ments

3 Answers 3

Reset to default 3

I 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

相关推荐

  • javascript - chai deep equal and equal not working - Stack Overflow

    Some how chai its not workingpromise = doSomething()expect(promise).to.eventually.equal(obj)expect(prom

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信