{"action":"created","issue":{"url":"https://api.github.com/repos/sinonjs/sinon/issues/657","repository_url":"https://api.github.com/repos/sinonjs/sinon","labels_url":"https://api.github.com/repos/sinonjs/sinon/issues/657/labels{/name}","comments_url":"https://api.github.com/repos/sinonjs/sinon/issues/657/comments","events_url":"https://api.github.com/repos/sinonjs/sinon/issues/657/events","html_url":"https://github.com/sinonjs/sinon/issues/657","id":55399885,"number":657,"title":"JSDom + fakeXHR troubles","user":{"login":"jmeas","id":2322305,"avatar_url":"https://avatars.githubusercontent.com/u/2322305?v=3","gravatar_id":"","url":"https://api.github.com/users/jmeas","html_url":"https://github.com/jmeas","followers_url":"https://api.github.com/users/jmeas/followers","following_url":"https://api.github.com/users/jmeas/following{/other_user}","gists_url":"https://api.github.com/users/jmeas/gists{/gist_id}","starred_url":"https://api.github.com/users/jmeas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmeas/subscriptions","organizations_url":"https://api.github.com/users/jmeas/orgs","repos_url":"https://api.github.com/users/jmeas/repos","events_url":"https://api.github.com/users/jmeas/events{/privacy}","received_events_url":"https://api.github.com/users/jmeas/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2015-01-25T06:01:59Z","updated_at":"2016-12-08T06:58:44Z","closed_at":"2015-01-26T21:15:23Z","body":"Sinon version: **v1.12.2**\nEnvironment: **OSX 10.10.1**\nExample URL: **N/A**\n\n**Description**\nI'm attempting to write unit tests for jQuery ajax requests in a JSDom environment, but Sinon's fake XHR is not intercepting the requests. The exact same suite **does** work in the browser.\n\nI know the jQueryXHR + JSDom combo can cause some folks trouble, but I'm fairly certain that Ive got them configured correctly. I've set the `global.window` and `global.XMLHttpRequest` object from JSDom so that jQuery can make XHR requests, and I've verified that this works. The issue comes in when I try to fake the requests with Sinon: they aren't intercepted.\n\nIt comes down to [`sinonXhr.supportsXHR`](https://github.com/cjohansen/Sinon.JS/blob/master/lib/sinon/util/fake_xml_http_request.js#L28) being calculated as false. This follows from [this line](https://github.com/cjohansen/Sinon.JS/blob/master/lib/sinon/util/fake_xml_http_request.js#L25), where it looks for `XMLHttpRequest` on the local variable `global`. That local variable is passed in [here](https://github.com/cjohansen/Sinon.JS/blob/master/lib/sinon/util/fake_xml_http_request.js#L614). In Node, `this` is an empty object, while `global` is where it **should** be looking at for the xml http request.\n\nIt seems unlikely to me that this would go unnoticed for so long, so maybe I'm configuring things oddly. Has anyone had success with jQuery+JSDom+Sinon's FakeXHRs?\n"},"comment":{"url":"https://api.github.com/repos/sinonjs/sinon/issues/comments/265667750","html_url":"https://github.com/sinonjs/sinon/issues/657#issuecomment-265667750","issue_url":"https://api.github.com/repos/sinonjs/sinon/issues/657","id":265667750,"user":{"login":"arogachev","id":8326201,"avatar_url":"https://avatars.githubusercontent.com/u/8326201?v=3","gravatar_id":"","url":"https://api.github.com/users/arogachev","html_url":"https://github.com/arogachev","followers_url":"https://api.github.com/users/arogachev/followers","following_url":"https://api.github.com/users/arogachev/following{/other_user}","gists_url":"https://api.github.com/users/arogachev/gists{/gist_id}","starred_url":"https://api.github.com/users/arogachev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arogachev/subscriptions","organizations_url":"https://api.github.com/users/arogachev/orgs","repos_url":"https://api.github.com/users/arogachev/repos","events_url":"https://api.github.com/users/arogachev/events{/privacy}","received_events_url":"https://api.github.com/users/arogachev/received_events","type":"User","site_admin":false},"created_at":"2016-12-08T06:58:44Z","updated_at":"2016-12-08T06:58:44Z","body":"I use [mocha-jsdom](https://github.com/rstacruz/mocha-jsdom) wrapper. In my case I predefined `sinon` variable without assigning value to it at the very top of test file:\r\n\r\n```js\r\nvar sinon;\r\n```\r\n\r\nThen I imported sinon to it after registering jsdom and testable code:\r\n\r\njsdom({src: fs.readFileSync('vendor/bower/jquery/dist/jquery.js', 'utf-8')});\r\n\r\n```js\r\nbefore(function () {\r\n    $ = window.$;\r\n    registerTestableCode();\r\n    sinon = require('sinon');\r\n});\r\n```\r\n\r\nThen in specific test suite where fake server is needed I wrote:\r\n\r\n```js\r\ndescribe('refresh', function () {\r\n    var server;\r\n\r\n    beforeEach(function () {\r\n        server = sinon.fakeServer.create();\r\n        window.XMLHttpRequest = global.XMLHttpRequest;\r\n    });\r\n\r\n    afterEach(function () {\r\n        server.restore();\r\n    });\r\n\r\n    // Testing code\r\n});\r\n```\r\n\r\nThis works fine, however for convenience and speed on dev server I included only one file in tests suite. Tests were passing, when I added the rest of the files back, the same error appeared:\r\n\r\n> TypeError: Cannot read property 'respond' of undefined\r\n\r\ncoming from line:\r\n\r\n```js\r\nserver.requests[0].respond(200, {\"Content-Type\": \"application/json\"}, JSON.stringify(response));\r\n```\r\n\r\nSo I had to import `sinon` in similar fashion in other files even there are no ajax related code at all and file with AJAX is tested **before ** the other ones.\r\n\r\nLooking for a better fix.\r\n\r\n\r\n"}}
{"actor":{"display_login":"arogachev"}}