Skip to content

Commit

Permalink
fix: recurse custom object-returning inspect (#39)
Browse files Browse the repository at this point in the history
* test: inspect with custom object-returning inspect

* fix: recursively inspect customInspect output

Co-authored-by: Keith Cirkel <[email protected]>
  • Loading branch information
flaambe and keithamus authored Feb 4, 2021
1 parent 8d6cc83 commit c66dc49
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function inspect(value, options) {
// If `options.customInspect` is set to true then try to use the custom inspector
if (customInspect && value) {
const output = inspectCustom(value, options, type)
if (output) return output
if (output) return inspect(output, options)
}

const proto = value ? Object.getPrototypeOf(value) : false
Expand Down
14 changes: 12 additions & 2 deletions test/acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('objects', () => {
const obj = {
inspect: () => 1,
}
expect(inspect(obj, { customInspect: true })).to.equal(1)
expect(inspect(obj, { customInspect: true })).to.equal('1')
})

it('uses a custom deeply nested inspect function if `customInspect` is turned on', () => {
Expand All @@ -39,6 +39,16 @@ describe('objects', () => {
inspect: (depth, options) => options.stylize('Object content', 'string'),
},
}
expect(inspect(obj, { customInspect: true })).to.equal('{ sub: Object content }')
expect(inspect(obj, { customInspect: true })).to.equal("{ sub: 'Object content' }")
})

it('inspect with custom object-returning inspect', () => {
const obj = {
sub: {
inspect: () => ({ foo: 'bar' }),
},
}

expect(inspect(obj, { customInspect: true })).to.equal("{ sub: { foo: 'bar' } }")
})
})

0 comments on commit c66dc49

Please sign in to comment.