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

Viewed 3287

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).

3 Answers

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

client: {
  captureConsole: false
}

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();

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

Related