mercredi 1 juillet 2015

How can I test unit test my jQuery-based code?

I have following code in a file named index.js. I want to test some functions in it add, print being the prime one.

(function($){
  "use strict";

  $(function(){

    var add = function(a, b){
    // ...
      },
    print = function(str){
    // ...
      },
      setup = function(){
    // ...
      },
      init = function(){
    // ...
      };

      setup();
      init();
  });
})(jQuery);

How can I do so? Does this way of coding add any security on client side? All I have is code that runs on the client side. No server involvement what-so-ever.

I tried :

var outerObj = (function(greet){ var innerObj = (function(){ return {test: function(){ console.log(greet); }} })(); return innerObj; })("hi");

outerObj.test();

but, in my case, innerObj line has a $ on the right hand of equal sign, making console yell error that shouts $(...) is not a function. Which I agree, it's an array of single document object.

Check out var a = (function(){ var val = $(function(){return 10;})(); })(); in any jquery enabled web-page's console, for the error.

Even if I ditch outer shell and bring $(function() {}); out. How am I gonna test that?

It's still an object, still an error.

What kind of testing can I perform, unit, bdd, etc. and how?

Aucun commentaire:

Enregistrer un commentaire