diff --git a/index.js b/index.js index bb3c2db..f6946da 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,10 @@ 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 inspect(output, options) + if (output) { + if (typeof output === 'string') return output + return inspect(output, options) + } } const proto = value ? Object.getPrototypeOf(value) : false diff --git a/test/acceptance.js b/test/acceptance.js index 444881c..915b8a0 100644 --- a/test/acceptance.js +++ b/test/acceptance.js @@ -28,9 +28,16 @@ describe('objects', () => { it('uses custom inspect function if `customInspect` is turned on', () => { const obj = { - inspect: () => 1, + inspect: () => 'abc', + } + expect(inspect(obj, { customInspect: true })).to.equal('abc') + }) + + it('inspects custom inspect function result', () => { + const obj = { + inspect: () => ['foobarbazbing'], } - expect(inspect(obj, { customInspect: true })).to.equal('1') + expect(inspect(obj, { customInspect: true, truncate: 15 })).to.equal("[ 'foobarba…' ]") }) it('uses a custom deeply nested inspect function if `customInspect` is turned on', () => { @@ -39,7 +46,7 @@ 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', () => {