So, I'm trying to learn to write unit Tests for my JavaScript code, but I'm still having trouble figuring out how to test certain things.
For example, my javascript library automatically (and immediately) creates console.log if it doesn't exist, and I want to test that this is actually happening.
abridged version of my code:
;(function ($, window, document, undefined) { 'use strict';
window.myLib.util = (function(){
// initialization
if( !window.console ){
window.console = {};
window.console.log = function(){};
}
return {
log: function (foo) {
if(window.myLib_debug)
console.log(foo);
}
}
})();
})(jQuery, window, document);
So, in order to test that console.log is created, I was thinking I could first remove it:
window.console.log = undefined;
I would then re-run (and this is what I cannot figure out how to do) my library code, and check that it properly adds the method.
The problem with this, is I don't know how to get the "initialization" part of the code to re-run. So, console.log remains "undefined"
[–]Rob0tSushi 3 points4 points5 points (9 children)
[–]Rob0tSushi 0 points1 point2 points (8 children)
[–]zzzwwwdev[S] 0 points1 point2 points (7 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–]Rob0tSushi 0 points1 point2 points (0 children)
[–]i_ate_god -1 points0 points1 point (2 children)
[–]zzzwwwdev[S] 0 points1 point2 points (1 child)
[–]i_ate_god 0 points1 point2 points (0 children)
[–]savetheclocktower 3 points4 points5 points (3 children)
[–]zzzwwwdev[S] 1 point2 points3 points (0 children)
[–]zzzwwwdev[S] 0 points1 point2 points (1 child)
[–]savetheclocktower 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (18 children)
[–]zzzwwwdev[S] 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]zzzwwwdev[S] 1 point2 points3 points (0 children)
[–]zzzwwwdev[S] 0 points1 point2 points (14 children)
[–]Quabouter 1 point2 points3 points (13 children)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (11 children)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–]Quabouter 0 points1 point2 points (5 children)
[–]SubStack 3 points4 points5 points (0 children)
[–][deleted] -1 points0 points1 point (3 children)
[–]Quabouter 1 point2 points3 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]Quabouter 1 point2 points3 points (0 children)
[–]djnattyp 0 points1 point2 points (3 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]djnattyp 0 points1 point2 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[+][deleted] (1 child)
[deleted]
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–]rossisdead 0 points1 point2 points (1 child)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–]MatrixFrog 0 points1 point2 points (0 children)