Search code examples
javascriptclient-sidequniterror-logging

Qunit Javascript client logging


we have small wrapper client logging framework to enable javascript client logging.We have exposed different function like LogError,LogWarn,LogInfo in Javascript.

       JavaScriptLogger.LogError(exception)            
       {
         postAjax(exception);
         WriteConsoleLog(exception)

   }

When ever user logs a error using JavaScriptLogger.LogError(exception),two things get executed: 1. postAjax(exception) : An ajax post is made to specified server URL. 2. WriteConsoleLog(exception) : Writes an exception Console.Error/Console.Warn/Console.Log,depending upon errortype passed by user.

Now I wanna have a unit test cases for LogError,LogWarn,LogInfo function using Qunit framework.

Could somebody provide me a suggestion how to start and how to go about.

Regards, SCP


Solution

  • Well, first off, I hope you understand that someone could flood your server logs this way, and hopefully you have something in place to prevent this.

    Second, you would want to mock that Ajax call so that you aren't relying on your server to work (eliminating one source of potential errors). You could use something like Mockjax for that.

    After that, you would want to probably run all of those tests in an asyncTest call (versus a simple test) since the Ajax call is still supposed to be asynchronous. http://api.qunitjs.com/asyncTest/

    (PS Sorry if my formatting is bad, typing this on my phone.)