Last Updated: February 25, 2016
·
1.489K
· avnerner

In NodeJS - Simplest possible Jquery Mock

When required to test a Jquery plugin, on the server, that is not doing any DOM manipulation, you might want to simply mock the jQuery extension point:

var JQuery = function(){};
global.jQuery = global.$ = new JQuery();
global.jQuery.fn  = JQuery.prototype;

With above lines in place, you can now simple create your plugin:

(function($){
  $.fn.myExtension=function(params){

    //Your code here
  }
})(jQuery);

This assumes you are not reusing any Jquery core methods.