lundi 27 janvier 2020

Jest and console.dir not displaying entire object

I am trying to display an error within a Jest test but it won't show me all of the levels.

import util from 'util'

describe('Module', () => {
  it('should show all levels WITHOUT util', () => {
    const large = {
      level1: {
        level2: {
          message: 'At level 2!',
          level3: {
            message: 'At level 3!',
            level4: {
              message: 'At level 4!',
              level5: {
                message: 'At level 5!',
              },
            },
          },
        },
      },
    }
    console.dir(large, { depth: 5 })
  })

  it('should show all levels with util', () => {
    const large = {
      level1: {
        level2: {
          message: 'At level 2!',
          level3: {
            message: 'At level 3!',
            level4: {
              message: 'At level 4!',
              level5: {
                message: 'At level 5!',
              },
            },
          },
        },
      },
    }
    console.dir(util.inspect(large, { depth: 5 }))
  })
})

This gives me the following results

    { level1: { level2: { message: 'At level 2!', level3: [Object] } } } { depth: 5 }

    { level1:
       { level2:
          { message: 'At level 2!',
            level3:
             { message: 'At level 3!',
               level4:
                { message: 'At level 4!', level5: { message: 'At level 5!' } } } } } }

I was under the impression console.dir should do this for me already but the first test I'm getting [Object] after depth 2

I've even created a simple test outside of Jest and it seems to work as expected for console.dir, can anyone explain why this happens? Does Jest override how dir behaves with its own default depth or something?

const large = {
  level1: {
    level2: {
      message: 'At level 2!',
      level3: {
        message: 'At level 3!',
        level4: {
          message: 'At level 4!',
          level5: {
            message: 'At level 5!',
          },
        },
      },
    },
  },
}
console.dir(large, { depth: 5 })

Results in...

{ level1:
   { level2:
      { message: 'At level 2!',
        level3:
         { message: 'At level 3!',
           level4:
            { message: 'At level 4!', level5: { message: 'At level 5!' } } } } } }

Aucun commentaire:

Enregistrer un commentaire