Use JSON.stringify to log objects

There are two major advantages to JSON.stringify over util.inspect:

1.) JSON.stringify defaults to recursively printing deeply nested
objects.

2.) JSON.stringify output is JSON, meaning it can be taken directly from
the output and used wherever JSON is accepted. util.inspect output is
JSON-like, but also includes other annotation such as the types of
various values, as well as functions on objects.
This commit is contained in:
Julian Simioni
2016-03-31 14:08:27 -04:00
parent e32a460ad0
commit 9dfcc20bd9
2 changed files with 8 additions and 2 deletions

View File

@ -97,7 +97,13 @@ describe('Log class', function () {
expect(Log.join(['foo', 'bar'])).to.eql('foo bar');
});
it('stringifies objects', function () {
expect(Log.join([{ foo: 'bar' }])).to.eql('{ foo: \'bar\' }\n');
expect(Log.join([{ foo: 'bar' }])).to.eql('{\n "foo": "bar"\n}\n');
});
it('fully stringifies deeply nested objects', function() {
var object = { foo: { bar: { baz: 'value' } } };
var expected = '{\n "bar": {\n "baz": "value"\n }\n}\n';
expect(Log.join(object)).to.eql(expected);
});
});