Spying on console.error() with Jasmine

20,442

Solution 1

You can spy on console.error like this:

beforeEach(function(){
  spyOn(console, 'error');
})

it('should print error to console', function(){
  yourApp.start();
  expect(console.error).toHaveBeenCalled();
})

Solution 2

You can override the standard console.error function like this:

//call the error function before it is overriden
console.error( 'foo' );

//override the error function (the immediate call function pattern is used for data hiding)
console.error = (function () {
  //save a reference to the original error function.
  var originalConsole = console.error;
  //this is the function that will be used instead of the error function
  function myError () {
    alert( 'Error is called. ' );
    //the arguments array contains the arguments that was used when console.error() was called
    originalConsole.apply( this, arguments );
  }
  //return the function which will be assigned to console.error
  return myError;
})();

//now the alert will be shown in addition to the normal functionality of the error function
console.error( 'bar' );

This solution works with Jasmin or anything else. Just put the code above before the other codes and any call after this to console.error() will call the overridden function.

Share:
20,442

Related videos on Youtube

Christian
Author by

Christian

Hi, I'm Christian I'm 32 and as of October 2018, I'll be enrolled in a bioinformatics master's degree. In my previous job, I've been working on the front- as well as the backend of a B2B web application for about five years. I like to learn and I like challenges. I'm a team player, but also very confident in working alone.

Updated on July 09, 2022

Comments

  • Christian
    Christian about 2 years

    I'm actually new to JavaScript as well as Jasmine. So it might be something really obvious that fixes my problem but I can't see it.

    I want to check if (an already existing) JavaScript application calls console.error() while loading. I don't really see a way how to realise this with Jasmine. I've included the JavaScript file as well as the spec file in the SpecRunner.html. But I take it that I somehow need to "instantiate" the application in order to test if it throws any errors on the console, right?

    Or should I include the SpecRunner.html code only for this purpose into the HTML code of the app?

  • Christian
    Christian over 11 years
    Yeah, that's what I was supposing. But this won't make it. It might have something to do with how the app is structured. But thanks for the help!
  • Christian
    Christian over 11 years
    Alright, I've checked again, made some tweaks and it worked. I guess all I needed was the assurance that this is the actual way it should work. Thanks again!
  • CSS
    CSS almost 9 years
    It's now more appropriate to use an .and.clickThrough(); or and.callFake(function(){ ... }); implementation rather than leaving the function naked, using Jasmine 2.x. Hope that helps. C§
  • ZackDeRose
    ZackDeRose over 5 years
    Not working for me - attempting to use toHaveBeenCalledWith() to assert that a specific warning message is output to console.warn. I see the proper warning in my Karma console, but the test is failing with: Expected spy warn to have been called with [ '*** desired error msg ***' ] but it was never called. Using just expect(console.warn).toHaveBeenCalled() also fails with: Expected spy warn to have been called..
  • D. Gibbs
    D. Gibbs over 4 years
    Make sure to put spyOn(console, 'error'); in the beforeEach method. I found that it works when placed there but not inside of the individual test
  • jake
    jake almost 3 years
    I tried spyOn(console, 'error'); in beforeEach and beforeAll and neither worked in Jasmine version 3.7.1
  • tryingHard
    tryingHard over 2 years
    @D.Gibbs someone knows why this happens?