Testing responses in node.js?

I just downloaded mocha.js and conducted some basic tests using expect.js to make sure it works correctly.

But what about testing the responses in my node application at a specific url? How can I check what response I get from switching to /users for example?

Using Expresso, the predecessor of mocha.js, I could do assert.response(server, req, res|fn[, msg|fn]) and test the answer.

+7
source share
1 answer

This is one thing that I like about Node.js / Javascript, making this kind of thing easy as soon as you hang it.

In short, you run your server code and then actually use Request or a super agent to make these HTTP requests. I personally prefer Superagent because of its automatic JSON encoding, but be careful, documents are outdated and incorrect, YMMV. Most people choose a request.

A simple mocha example using a query:

 describe('My Server', function(){ describe('GET /users', function(){ it("should respond with a list of users", function(done){ request('http://mytesturl.com/users', function(err,resp,body){ assert(!err); myuserlist = JSON.parse(body); assert(myuserlist.length, 12); done(); }); }); }); }); 

Hope this helps. Here is an example of my Mocha test (CoffeeScript) using this style with detailed examples: https://github.com/jprichardson/logmeup-server/blob/develop/test/integration/app.test.coffee Oh, I also use Superagent.

+6
source

All Articles