Search code examples
vue.jsvitest

Vitest: How to increase depth of console.log?


I am using Vitest and in my testing I want to console log output something. But If I log like this:

console.log({ a: { b: { c: { d: { e: 1 } } } } });

I get this output:

{ a: { b: { c: [Object] } } }

The same happens with console.dir(). Is there any way to increase the depth of object logging in Vitest? I am working around it with JSON.stringify() but it would be helpful to have it formatted like the default.


Solution

  • From this question:

    Using 250R answer:

    You need to use util.inspect():

    const util = require('util')
    
    console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}))
    
    // alternative shortcut
    console.log(util.inspect(myObject, false, null, true /* enable colors */))
    

    Outputs:

    { a: 'a',  b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
    

    Or you can find other ways of doing the same thing on that question page; you can pick whatever you feel comfortable with.


    But if I were you, I'd go with:

    console.dir(myObject, { depth: null });
    

    because of the following:

    1. Built-in function from console, so no dependencies are needed.
    2. Support colorization.
    3. No hard coded functions.