javascript - Stubbing jQuery.ajax in node environment (jQuery 2.x) -


i trying run tests require stubbing jquery.ajax. i'm using sinonjs , used work fine older version of jquery (1.x)

var $ = require('jquery'); var sinon = require("sinon"); sinon.stub($, "ajax"); // worked because $.ajax defined 

however, after upgrading jquery 2.x, have had include window environment when require jquery module run. using jsdom accomplish this:

var document = require('jsdom').jsdom(),     window  = document.parentwindow,     $       = require('jquery')(window); 

problem $.ajax undefined. suspect because returns jquery object bound specific element not entirely sure. know why , how around this?

edit buddy of mine isn't on has pointed out if attach window global, can plain jquery object instead of factory

    global.window = require('jsdom').jsdom().parentwindow;     var $ = require('jquery'); // works $.ajax defined 

i'm not fan of attaching window global affect of plugins type check window. not blocker, i'd love see if there other way go around problem.

i have sworn after reading jquery source, tried on day asked question didn't work. tried again , it's working.

tl;dr jquery attaches $ window namespace browser emulator.

var document    = require('jsdom').jsdom(),     window      = document.parentwindow; require('jquery')(window); var $ = window.$; 

hopefully it's useful else.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -