Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Jest log the entire error object?

Jest is shortening error logs in the following way:

GraphQLError {
    message: 'Cannot set property \'marketCap\' of undefined',
    locations: [ { line: 3, column: 11 } ],
    path: [ 'listings' ],
    extensions: 
        { code: 'INTERNAL_SERVER_ERROR',
            exception: { stacktrace: [Array] }
        }
} 0 [
    GraphQLError {
        message: 'Cannot set property \'marketCap\' of undefined',
        locations: [ [Object] ],
        path: [ 'listings' ],
        extensions: { code: 'INTERNAL_SERVER_ERROR', exception: [Object] }
    }
]

How can I force it to print the entire error instead of using [Array] and [Object]?

like image 460
Marcos Pereira Avatar asked Feb 16 '19 17:02

Marcos Pereira


People also ask

How do I print a full object in console log?

Answer: Use console. log() or JSON. stringify() Method This method will print the object in browser console.

Does console log work in jest?

Jest by default prints all console. log (warnings, errors, etc) messages to the console. That's great - it helps you understand what's going on in your code when tests run.

How do I avoid console error in jest?

If using Jest test framework, you can turn off all console messages by one command: jest --silent .

How do you log an object in node JS?

Logging objects in Node. jslog() into a JavaScript program that runs in the browser, that is going to create a nice entry in the Browser Console: Once you click the arrow, the log is expanded, and you can clearly see the object properties: In Node, the same happens.


2 Answers

I would assume it is not a jest but a normal nodeJS logging issue. Can be solved by using:

const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
like image 82
Andreas Köberle Avatar answered Oct 28 '22 11:10

Andreas Köberle


Thank to @andreas answer I was able to add this to my jest.setup.js file:

var util = require('util');
util.inspect.defaultOptions.depth = null; //enable full object reproting in console.log
like image 38
Gomino Avatar answered Oct 28 '22 09:10

Gomino