visionmedia/mocha — pull request #1079 by papandreou (2013-12-20 13:47:47+00:00)
Title: Canonicalize objects before stringifying and diffing them

Assertion libraries can set `showDiffs` to true, which will serialize objects, then do a string diff -- effectively a poor man's object diff. However, it produces an unnecessarily big diff when the key definition order in objects are different:

```javascript
var expect = require('unexpected');

describe('the thing', function () {
it('should DTRT', function () {
expect({foo: 123, bar: 456, quux: 789}, 'to equal', {quux: 78, foo: 123, bar: 456});
});
});
```

```
0 passing (5ms)
1 failing

1) the thing should DTRT:

+ expected - actual

{
+ "quux": 78,
"foo": 123,
+ "bar": 456
- "bar": 456,
- "quux": 789
}
```

This patch canonicalizes objects before comparing them so the above example produces:

```
0 passing (5ms)
1 failing

1) the thing should DTRT:

+ expected - actual

{
"bar": 456,
"foo": 123,
+ "quux": 78
- "quux": 789
}
```

... which I've found to be a great help when diffing large objects.