7

是否有一个断言库可以向我展示两个对象在深入比较时有什么区别?

我尝试过使用 chai,但它只是告诉我对象是不同的,但不是在哪里。节点的断言也是如此......

4

4 回答 4

3

Substack 的difflet可能是你需要的

更新:但是等等,还有更多:https ://github.com/andreyvit/json-diff https://github.com/algesten/jsondiff https://github.com/samsonjs/json-diff

于 2012-12-10T02:56:44.157 回答
2

使用 chai 1.5.0 和 mocha 1.8.1,以下对我有用:

var expect = require('chai').expect;

it("shows a diff of arrays", function() {
  expect([1,2,3]).to.deep.equal([1,2,3, {}]);
});

it("shows a diff of objects", function() {
  expect({foo: "bar"}).to.deep.equal({foo: "bar", baz: "bub"});
});

结果是:

✖ 2 of 2 tests failed:

1)  shows a diff of arrays:

  actual expected

  1 | [
  2 |   1,
  3 |   2,
  4 |   3,
  5 |   {}
  6 | ]

2)  shows a diff of objects:

  actual expected

  {
    "foo": "bar",
    "baz": "bub"
  }

此处未显示的是输出以红色/绿色突出显示,其中行意外/缺失。

于 2013-03-10T01:26:48.430 回答
2

基于这个 StackOverflow 答案,我相信这个问题发生在我身上,因为我的测试是异步的。

通过使用以下模式,我让差异再次正常工作:

try {
  expect(true).to.equal(false);
  done();  // success: call done with no parameter to indicate that it() is done()
} catch(e) {
  done(e);  // failure: call done with an error Object to indicate that it() failed
}
于 2014-06-11T17:23:04.770 回答
2

是的,有:assert-diff

你可以像这样使用它:

var assert = require('assert-diff')

it('diff deep equal with message', function() {
  assert.deepEqual({pow: "boom", same: true, foo: 2}, {same: true, bar: 2, pow: "bang"}, "this should fail")
})

结果是:

1) diff deep equal with message:
     AssertionError: this should fail
 {
-  bar: 2
+  foo: 2
-  pow: "bang"
+  pow: "boom"
 }
于 2016-08-19T22:36:02.823 回答