sinon icon indicating copy to clipboard operation
sinon copied to clipboard

Object has no method 'useFakeXMLHttpRequest'

Open vevo-kurt-snyder opened this issue 12 years ago • 10 comments

I can't set up a an XHR fake -- here's my code:

console.dir sinon xhr = sinon.useFakeXMLHttpRequest()

The first line indicates I've got the module -- output is: { wrapMethod: [Function: wrapMethod], extend: [Function: extend], create: [Function: create], deepEqual: [Function: deepEqual], functionName: [Function: functionName], functionToString: [Function: toString], getConfig: [Function], ...

But I can't actually call the function:

TypeError: Object # has no method 'useFakeXMLHttpRequest'

vevo-kurt-snyder avatar Aug 21 '14 23:08 vevo-kurt-snyder

Which environment is this failing in? A browser? Which one(s)? How are you loading Sinon? Are you using a pre-built or source version of Sinon?

mroderick avatar Aug 22 '14 16:08 mroderick

Each Mocha spec calls require("./testsCommon").common and testsCommon.js runs an initialization function that calls global.sinon = require("sinon"). Sinon is installed via npm install, and its package.json identifies it as 1.10.3.

Mocha specs are run in the console, on Mavericks, with mocha tests/js/*.js

When I view the sinon object I see a method 'userFakeXMLHttpRequest' in the sandbox object. Thanks.

vevo-kurt-snyder avatar Aug 25 '14 18:08 vevo-kurt-snyder

XMLHttpRequest is not available on Node. It's a browser feature. What are you trying to do?

mantoni avatar Aug 25 '14 20:08 mantoni

There's no Node server here, just loading Node packages. The tests are using package xmlhttprequest version 1.6, so I do have XMLHttpRequest. The actual app runs in a non-standard Javascript environment (not a browser) but that's not at issue here. My question is only about the test environment: I am trying to mock XHR responses in the Mocha tests I have running in the console.

vevo-kurt-snyder avatar Aug 25 '14 20:08 vevo-kurt-snyder

You will have to be more specific about your environment and how it works. Sinon does feature detection for things like XMLHttpRequest.

Please send a bug report that includes all the details and how to reproduce the issue.

mantoni avatar Aug 25 '14 21:08 mantoni

Here's a sample app with the test that illustrates the problem: https://github.com/ksnyder/testSample.git Npm install was already run; run mocha tests/js/*.js to see the test failure.

It's puzzling because sinon is available, its package.json says 1.10.3, but the module has no method useFakeXMLHttpRequest except within the sandbox object (which doesn't work either).

Thx.

vevo-kurt-snyder avatar Aug 26 '14 19:08 vevo-kurt-snyder

I'm running into the same issues here with running Jasmine tests (in browser).

I'm stepping through fake_xml_http_request.js, and apparently the parameter global is undefined inside the function. It's an IIFE, the value passed into it is an evaluated function.

I stepped through that function

(function(){ return typeof global === "object" ? global : this; })()

In here, global is undefined, meaning this function should return this, which it does. this is a reference to the window object.

Somehow, that value is lost when passed into the main function. On the first line, the value of global is undefined.

josephspens avatar Sep 01 '14 17:09 josephspens

Replacing the IIFE with the return value seems to fix it, all my tests went from failing to passing.

Before

(function(global) {
  ...
})((function(){ return typeof global === "object" ? global : this; })())

After

(function(global) {
  ...
})(typeof global === "object" ? global : this)

I wasn't sure where to open a PR since master is out of sync with 1.10.3

josephspens avatar Sep 01 '14 18:09 josephspens

Taken me a while to get my head around the variety of ways people might be expecting execution environments to play out, what with Sinon's source being couched in boilerplate catering for environment and module pattern inference. I'm using Sinon via mochify, which creates a convenient interface for marrying Browserify > Mocha > PhantomJS – for those who aren't familiar, Browserify parses and pre-processes Javascript in Node and packages it for browser-space execution: the key use-case being the ability to modularize code intended for the browser with require dependency chains.

So I'm requireing sinon in Node for eventual execution in a PhantomJS environment. The problem is that Sinon's immediately-invoked environment inference and feature detection executes in Node, fails to pick up any of the native XHR methods, and as such never instantiates useFakeXMLHttpRequest.

The only solution I can think of (for my particular case, where Sinon is packaged in Node to be run in the browser) is to fork Sinon to use a less duck-typey modularization pattern such that the user controls when feature detection & conditional initialization occurs.

barneycarroll avatar Oct 14 '14 11:10 barneycarroll

This should have been addressed in Sinon 2 with the CommonJS refactoring. If this is still an issue, please shout and we'll reopen.

mantoni avatar May 22 '17 07:05 mantoni