How do you log an objects details to the test console with `cy.log`?

Viewed 48

When you do something like the following:

const someObject = {
    key: 1,
    anotherKey: 'test'
}
cy.log("Some object", someObject)

It will be rendered in the Cypress UI as Some object, Object{2}

Is there a way to get this to print prettier?

I am on version 10.3.0 here is a result with my actual log message: enter image description here

Actual code:

        const loginInput = {
          userContext: "global_",
          email: finalEmail,
          password,
          verificationCode,
        };

        cy.log("Authenticating via GraphQL with the following details", loginInput);
1 Answers

I ran the same code, and it is showing the key-value pairs in the log.

test runner screenshot

You can use the Javascript Method Object.entries() to loop over the key-value pair and log them one by one.

const someObject = {
  key: 1,
  anotherKey: 'test',
  anotherKey2: 'test2',
}
Object.entries(someObject).forEach(([key, value]) => {
  cy.log(key, value)
})

Test runner 2

Related