import { inspect } from 'node:util'; import { describe, expect, it } from 'vitest'; import { inspectValue } from './inspect-value.js'; describe('inspectValue', () => { it('formats values JSON.stringify throws on', () => { const circular: Record = {}; circular.self = circular; expect(inspectValue(1n)).toBe('1n'); expect(inspectValue(Symbol('x'))).toBe('Symbol(x)'); expect(inspectValue(circular)).toBe(' { self: [Circular *1] }'); }); it('formats primitives and plain objects', () => { expect(inspectValue('bogus')).toBe("'bogus'"); expect(inspectValue(null)).toBe('null'); expect(inspectValue(undefined)).toBe('undefined'); expect(inspectValue({ status: 503 })).toBe('{ status: 503 }'); }); it('describes a function by its name, never its source', () => { function leakyHandler() { return 'sk_live_secret'; } expect(inspectValue(leakyHandler)).toBe('[Function: leakyHandler]'); }); it("does not run the value's own code", () => { let ran = false; const hostile = { get secret() { ran = true; throw new Error('getter exploded'); }, toString() { ran = true; throw new Error('no string'); }, toJSON() { ran = true; throw new Error('no json'); }, [inspect.custom]() { ran = true; throw new Error('no inspect'); }, }; const proxy = new Proxy( {}, { get() { ran = true; throw new Error('trap'); }, ownKeys() { ran = true; throw new Error('trap'); }, } ); expect(inspectValue(hostile)).toContain('secret: [Getter]'); expect(inspectValue(proxy)).toContain('{}'); expect(ran).toBe(false); }); it('bounds the output size', () => { expect(inspectValue('x'.repeat(1000)).length).toBeLessThan(300); expect(inspectValue(Array.from({ length: 100 }, (_, i) => i))).toContain( '... 90 more items' ); expect(inspectValue({ a: { b: { c: { d: 1 } } } })).toBe( '{ a: { b: { c: [Object] } } }' ); }); });