Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.log dont work correctly on Karma/Jasmine

I have a structure as

myObject={
   Items:[
   {
    namePizza:{}
    quantity: int
   }
   {
    namePizza:{}
    quantity: int
   }
  ]
}

If a try console.log(myObject) Karma always return LOG:{Items:[]}

I dont understand why happen it.

like image 949
Ramsés Fernández Avatar asked Dec 10 '25 01:12

Ramsés Fernández


1 Answers

A normal console.log will show you the inner content of the the javascript objects

> console.log({1,2})
> Object { 1, 2 }

BUT why does calling console.log through karma behaves differently!

Digging into the karma code; Karma modifies the original console to extract the messages and add metadata to each log. So in order to have a safe logger and to be able to log everything given to them as arguments, these libraries stringify the data you pass to the logger.

Here is what is exactly happening in karma logger:

values.push(this.stringify(args[i], 3))

as you can see, they have their own implementation of the logger.

The modified logger calls stringify against the data you send.

You can check the karma logger where the "stringifying" happens here: https://github.com/karma-runner/karma/blob/master/context/karma.js#L16

Practically most test runners or logging transports (example ravenJS for sentry) do such things.

To solve this issue; wrap your object with JSON.stringify when calling console.log

Note that JSON.stringify will not work with Sets or Maps and their weak counterpart. For those you have to call .values() against them to get the logger working correctly!

like image 160
Bamieh Avatar answered Dec 11 '25 15:12

Bamieh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!