Skip to content

Commit

Permalink
fix(data-point): replace util.inspect with json-stringify-safe (#347)
Browse files Browse the repository at this point in the history
this change is to remove util.inspect inconsistencies across node versions

close #346
  • Loading branch information
acatl authored May 1, 2019
1 parent 35b18cc commit e112c99
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ exports[`resolve it should omit options.auth when encountering an error 1`] = `
Entity info:
- Id: request:a9
- options: { auth: '[omitted]',
method: 'GET',
json: true,
url: 'http://remote.test/source1' }
- options: {
\\"auth\\": \\"[omitted]\\",
\\"method\\": \\"GET\\",
\\"json\\": true,
\\"url\\": \\"http://remote.test/source1\\"
}
- value: {}
Request:
- message: '404 - undefined'
- message: \\"404 - undefined\\"
- statusCode: 404
- options: { auth: '[omitted]',
method: 'GET',
json: true,
url: 'http://remote.test/source1',
resolveWithFullResponse: true,
callback: [Function: RP$callback],
transform: undefined,
simple: true,
transform2xxOnly: false }
- options: {
\\"auth\\": \\"[omitted]\\",
\\"method\\": \\"GET\\",
\\"json\\": true,
\\"url\\": \\"http://remote.test/source1\\",
\\"resolveWithFullResponse\\": true,
\\"simple\\": true,
\\"transform2xxOnly\\": false
}
"
`;

Expand All @@ -31,22 +33,24 @@ exports[`resolveRequest log errors when request fails 1`] = `
Entity info:
- Id: test:test
- options: { json: true,
url: 'http://remote.test/source1',
auth: '[omitted]' }
- value: 'foo'
- options: {
\\"json\\": true,
\\"url\\": \\"http://remote.test/source1\\",
\\"auth\\": \\"[omitted]\\"
}
- value: \\"foo\\"
Request:
- error: 'not found'
- message: '404 - \\"not found\\"'
- error: \\"not found\\"
- message: \\"404 - \\\\\\"not found\\\\\\"\\"
- statusCode: 404
- options: { json: true,
url: 'http://remote.test/source1',
resolveWithFullResponse: true,
callback: [Function: RP$callback],
transform: undefined,
simple: true,
transform2xxOnly: false,
auth: '[omitted]' }
- options: {
\\"json\\": true,
\\"url\\": \\"http://remote.test/source1\\",
\\"resolveWithFullResponse\\": true,
\\"simple\\": true,
\\"transform2xxOnly\\": false,
\\"auth\\": \\"[omitted]\\"
}
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ exports[`validateModifiers It should include base modifiers 1`] = `
Valid properties for this entity are:
inputType, before, value, after, outputType, error, params, bar
Please review your entity and make any necessary corrections so it can be parsed:
'foo:test': { inputType: true,
outputType: true,
before: true,
after: true,
error: true,
value: true,
foo: true }"
'foo:test': {
\\"inputType\\": true,
\\"outputType\\": true,
\\"before\\": true,
\\"after\\": true,
\\"error\\": true,
\\"value\\": true,
\\"foo\\": true
}"
`;

exports[`validateProperties It should throw error if keys dont match 1`] = `
Expand All @@ -21,7 +23,9 @@ exports[`validateProperties It should throw error if keys dont match 1`] = `
Valid properties for this entity are:
bar
Please review your entity and make any necessary corrections so it can be parsed:
'foo:test': { foo: true }"
'foo:test': {
\\"foo\\": true
}"
`;

exports[`validateProperties It should throw error if keys dont match 2`] = `
Expand All @@ -30,5 +34,8 @@ exports[`validateProperties It should throw error if keys dont match 2`] = `
Valid properties for this entity are:
bar
Please review your entity and make any necessary corrections so it can be parsed:
'foo:test': { bar: true, foo: true }"
'foo:test': {
\\"bar\\": true,
\\"foo\\": true
}"
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Util = require('util')
const _ = require('lodash')
const stringify = require('json-stringify-safe')

/**
* @throws Errors when it finds an invalid key
Expand All @@ -18,7 +19,7 @@ function validateProperties (id, spec, validKeys) {
differentKeys.join(', '),
validKeys.join(', '),
id,
Util.inspect(spec)
stringify(spec, null, 2)
)
)
}
Expand Down
16 changes: 15 additions & 1 deletion packages/data-point/lib/utils/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`inspect It should log data 1`] = `
Array [
"
[33minspect[0m:",
"test:test",
"
foo:",
"\\"bar\\"",
]
`;

exports[`inspectProperties It should add indent to keys 1`] = `
" - a: 1
- b: true
Expand All @@ -8,6 +19,9 @@ exports[`inspectProperties It should add indent to keys 1`] = `

exports[`inspectProperties It should inspect each property 1`] = `
"- a: 1
- b: { a: true, b: false }
- b: {
\\"a\\": true,
\\"b\\": false
}
"
`;
8 changes: 3 additions & 5 deletions packages/data-point/lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require('lodash')
const util = require('util')
const stringify = require('json-stringify-safe')

/**
* sets key to value of a copy of target. target stays untouched, if key is
Expand Down Expand Up @@ -55,7 +55,7 @@ function inspect (acc, data) {
log.push('\n\x1b[33minspect\x1b[0m:', _.get(acc, 'reducer.spec.id'))
for (let key in data) {
const value = data[key]
log.push(`\n${key}:`, _.attempt(JSON.stringify, value, null, 2))
log.push(`\n${key}:`, stringify(value, null, 2))
}

console.info.apply(null, log)
Expand All @@ -72,9 +72,7 @@ function inspectProperties (obj, props, indent = '') {
return props.reduce((acc, key) => {
const val = obj[key]
if (typeof val !== 'undefined') {
return `${acc}${indent}- ${key}: ${util.inspect(obj[key], {
breakLength: 60
})}\n`
return `${acc}${indent}- ${key}: ${stringify(obj[key], null, 2)}\n`
}
return acc
}, '')
Expand Down
3 changes: 1 addition & 2 deletions packages/data-point/lib/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ describe('inspect', () => {
test('It should log data', () => {
console.info = jest.fn()
utils.inspect(getAcc(), { foo: 'bar' })
expect(console.info.mock.calls[0]).toContain('\nfoo:')
expect(console.info.mock.calls[0]).toContain('"bar"')
expect(console.info.mock.calls[0]).toMatchSnapshot()
})
})

Expand Down

0 comments on commit e112c99

Please sign in to comment.