Well there are a lot of problems. Firstly, you are trying to require sinon how it works in node, but it does not work in casper, because casper does not care if you have a node_modules directory or not, and it does not look into it. I assume that you installed sinon in the node_modules directory for you to do this:
var sinon = require('./node_modules/sinon');
The trick is that you can only use relative paths to install modules in node_modules, because for casper there is no such thing as node_modules directory resolution.
In the next part, which you are doing wrong, it looks like you are confused between the phantomjs side and the client side. the script you have above is evaluated on the phantomjs side, and the scripts included in the html are evaluated on the client side. These two do not share memory with each other, global objects are different. So you cannot do sinon.fakeServer.create(); on the phantomjs side, because it is trying to create fake XMLHttpRequest , but this does not exist on the phantomjs side, it exists on the client side. So technically you do not need to run it here.
So what you need to do is evaluate the client-side sinon module, and also evaluate the script that you have on the client side.
This leads us to the following code:
var url = 'http://localhost:3000/'; // Patch the require as described in // http://docs.casperjs.org/en/latest/writing_modules.html#writing-casperjs-modules var require = patchRequire(require); var casper = require('casper').create({ clientScripts: [ // The paths have to be relative to the directory that you run the // script from, this might be tricky to get it right, so play with it // and try different relative paths so you get it right 'node_modules/sinon/pkg/sinon.js', 'node_modules/sinon/pkg/sinon-server-1.7.3.js' ] }); casper.test.begin('integration',1,function suite(test){ casper.start(url,function start(){ test.assertHttpStatus(200,'http status is 200'); casper.evalute(function () { var server = sinon.fakeServer.create() server.respondWith("GET", "/login", [200, { "Content-Type": "application/json" },'{"id": 12}']); }); }); casper.run(function run(){ test.done(); }); });
Please note that I did not include the var sinon = require('./node_modules/sinon'); because it is no longer needed as we evaluate client-side sinon.