Steph M
Steph M

Reputation: 2172

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

Upvotes: 7

Views: 4917

Answers (3)

Omkar Porje
Omkar Porje

Reputation: 288

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

Upvotes: 5

MkMan
MkMan

Reputation: 2191

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

Upvotes: 3

Vitor M. Barbosa
Vitor M. Barbosa

Reputation: 3646

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

client: {
  captureConsole: false
}

Upvotes: 7

Related Questions