I'm having trouble displaying an error in my Jest test because it's not showing all the levels as expected.
import util from 'util'
describe('Module', () => {
it('should display 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 display 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 is the output I receive:
{ 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 thought that console.dir should automatically handle this for me, but in the first test, after a depth of 2, I encounter [Object]
.
I even conducted a simple test outside of Jest and the console.dir function seemed to work fine. Can someone explain why this discrepancy occurs? Could Jest be overriding the default behavior of dir with its own depth settings?
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 })
The result is...
{ level1:
{ level2:
{ message: 'At level 2!',
level3:
{ message: 'At level 3!',
level4:
{ message: 'At level 4!', level5: { message: 'At level 5!' } } } } } }