Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to suppress console.log's in Jasmine tests?

I am running some jasmine tests on a function that is logging something. Each time I run the tests I see that log in the output of the tests. I have quite a few logs in my functions that I am testing and didn't see a way to suppress the logs in the jasmine output.

My actual tests are spying to make sure that console.log is being called with the correct string.

Suppressing the logs in the jasmine output is really more for testing aesthetics (I just like to see a nice clean green passing and not all the logs).

like image 264
Steph M Avatar asked Sep 20 '18 16:09

Steph M


3 Answers

If you are running your tests with karma, edit your karma.config.js and add:

client: {
  captureConsole: false
}
like image 157
Vitor M. Barbosa Avatar answered Oct 16 '22 12:10

Vitor M. Barbosa


You can put spy on the console method and expect it to have been called, This is how I am using it in my Jasmine Unit test cases. Hope it helps(replace 'warn' with 'log')

    spyOn(console, 'warn');
    fixture.detectChanges();
    component.doSomething(dummyEventObj);
    fixture.detectChanges();
    expect(console.warn).toHaveBeenCalled();
like image 40
Omkar Porje Avatar answered Oct 16 '22 12:10

Omkar Porje


Make sure your spy isn't calling the real console.log(). Something like this should do the trick spyOn(console, 'log');.

like image 1
M Mansour Avatar answered Oct 16 '22 13:10

M Mansour