nodejs深度与差异相等

是否有一个断言库,当深入比较时,会告诉我两个对象之间有什么区别?

我试过使用柴,但它只是告诉我,对象是不同的,但不是在哪里。 同样的事情节点的断言….

使用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" } 

这里没有显示的是,输出突出显示为红色/绿色,线路意外/缺失。

Substack的difflet可能是你需要的

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

基于这个StackOverflow的答案 ,我相信这个问题发生在我身上,因为我的testing是asynchronous的。

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

 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 } 

是的,有: 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" }