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:
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user