Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot call method 'create' of undefined. When calling sinon.fakeServer #125

Closed
tomkoptel opened this issue May 3, 2012 · 15 comments
Milestone

Comments

@tomkoptel
Copy link

I`m trying to initialize fakeServer instance while I have sinon required in my env. Here goes my setup:

helper.js

 window = require("jsdom").jsdom().createWindow();
 $ = require('jquery');
 require('expectThat.mocha');
 expect = require('chai').expect;

 sinon = require("sinon");

In my spec I call:

my_test.coffee

require('./helper.js')


describe "Given form to submit", ->
  server = undefined

  beforeEach ->
    server = sinon.fakeServer.create()

When Im executing codesinon.fakeServerreturnsundefined`. How can I manage to force fake server feature to work?

P.S. Im newbie in the JS testing. Please, dont judge me:)

@cjohansen
Copy link
Contributor

Unfortunately, the fakeServer is not included by default when on Node. You'll have to require it, which can get slightly nasty :/

var fakeServer = require(require.resolve("sinon").replace(".js", "/util/fake_server.js"));

@TEHEK
Copy link
Contributor

TEHEK commented May 24, 2012

You can also do

var fakeServer = require('sinon/lib/sinon/util/fake_server')

both will fail if lib structure changes with new update, but this one looks a bit cleaner :)

@frankrousseau
Copy link

When I do that I obtain a new error :

TypeError: Object #<Object> has no method 'useFakeXMLHttpRequest'

And

require('sinon/lib/sinon/util/fake_xml_http_request')

leads to :

sinon.xhr = { XMLHttpRequest: this.XMLHttpRequest };
^
ReferenceError: sinon is not defined

@cjohansen
Copy link
Contributor

The server isn't really designed to work on node at this point. You can hack it in place the following way, but note that it only fakes XMLHttpRequest, so that has to be somehow defined up front.

global.sinon = require("sinon");
require("sinon/lib/sinon/util/fake_xml_http_request");

@frankrousseau
Copy link

I forgot to mention that I use it with Brunch Test feature. http://brunch.io/
This allows you to launch your front-end tests from the command line but I'm not sure of how it works (it looks that they load your front app inside jsdom, then they run nodejs mocha tests on it).

@opnsrce
Copy link

opnsrce commented Oct 2, 2012

@cjohansen I noticed that there is an issue set for milestone 1.4.0 to make fakeServer override http.request in node. Is this still planned? #21

@cjohansen
Copy link
Contributor

It didn't make it in for 1.4. I honestly don't know when I'll have time to
do that.

On Wed, Oct 3, 2012 at 1:49 AM, Levi Hackwith notifications@github.comwrote:

@cjohansen https://github.com/cjohansen I noticed that there is an
issue set for milestone 1.4.0 to make fakeServer override http.request in
node. Is this still planned? #21#21


Reply to this email directly or view it on GitHubhttps://github.com//issues/125#issuecomment-9091409.

@rklancer
Copy link

Running in Node with the xmlhttprequest module installed and the fake_xml_http_request successfully required, I find the same error as @frankrousseau when executing sinon.fakeServer.create():

TypeError: Object #<Object> has no method 'useFakeXMLHttpRequest'

I believe this is caused by the var declaration in fake_server.js: https://github.com/cjohansen/Sinon.JS/blob/706ab5b78e6eb8ca5b15aa7917d7c91896e27775/lib/sinon/util/fake_server.js#L20

Even though sinon is global, the var declaration is hoisted, causing the global sinon to be shadowed, and undefined, when the typeof test is made. So locally sinon is assigned to an empty object which does not have the method useFakeXMLHttpRequest referenced here: https://github.com/cjohansen/Sinon.JS/blob/706ab5b78e6eb8ca5b15aa7917d7c91896e27775/lib/sinon/util/fake_server.js#L83

In fact, I find that locally removing the var declaration allows sinon.fakeServer.create() to complete without error.

@adborden
Copy link

Here's an additional use case:

We use Browserify to build our app so that we can write modules that can be shared between server and the client. For the client app, the tests are run in Karma, a browser environment. So, despite having an environment where fakeServer would work, requiring sinon won't pull in fakeServer, and requiring fakeServer won't setup the right binding to sinon.

@mroderick
Copy link
Member

@adborden can you open a separate ticket that describes the issue that you're experiencing?

If you feel the issue is relevant to this one, please mention this one and GitHub will create links between them.

@StephenGrider
Copy link

@mroderick fakeServer is still not functioning in a node environment, but I believe vojtechs fix does work. Can you reopen this issue?

@mroderick
Copy link
Member

@StephenGrider ok, I'll re-open it. We will have to investigate further

@mroderick mroderick reopened this Aug 20, 2014
@arif-dewi
Copy link

I have the same problem - sinon.fakeServer == undefined (removing var from fake_server.js didn`t help)

@jonnyreeves
Copy link
Contributor

If I understand the root cause of this issue correctly, this should be resolved as part of the CommonJS-ification of the Sinon internals that is being undertaken as part of the v2.0 release. See #966.

@fatso83
Copy link
Contributor

fatso83 commented Jul 11, 2016

Verified to be working in the node shell:

> sinon = require("./lib/sinon");
> server = sinon.fakeServer.create()
> Object.keys(server.__proto__)
[ 'create',
  'configure',
  'addRequest',
  'getHTTPMethod',
  'handleRequest',
  'logger',
  'logError',
  'log',
  'respondWith',
  'respond',
  'processRequest',
  'restore',
  'xhr' ]

@fatso83 fatso83 closed this as completed Jul 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests