I am trying to run some tests that require stubbing jQuery.ajax. I'm using SinonJS to do that and it used to work fine with older version of jQuery (1.x)
var $ = require('jquery');
var sinon = require("sinon");
sinon.stub($, "ajax"); // this worked because $.ajax is defined
However, after upgrading to jQuery 2.x, I have had to include a window environment when I require jquery from my module for it to run. I am using jsdom to accomplish this:
var document = require('jsdom').jsdom(),
window = document.parentWindow,
$ = require('jquery')(window);
PROBLEM $.ajax is now undefined. I suspect because now it returns the jQuery object bound to a specific element but not entirely sure. Does anyone know why and how to get around this?
EDIT A buddy of mine who isn't on SO has pointed out that if we attach window to global, we can get the plain jquery object instead of the factory
global.window = require('jsdom').jsdom().parentWindow;
var $ = require('jquery'); // this works as $.ajax is now defined
I'm not a fan of attaching window to global as it will affect up some of the plugins which type check window. Not a blocker, but I'd love to see if there is any other way to go around this problem.
jsdomto 3.x so that shouldn't have been a problem.$.ajaxdefined. It's just not working. I don't have this function bound to my jQuery object.