WHITECOLOR
WHITECOLOR

Reputation: 26142

Unit Tests of Node.js REST services with Mocha or Jasmine

I have node module that exports REST methods for express.js (index, show, update, delete)..

What is the best approach to test mocha or jasmine unit test framework?

How should I describe my test?

Thanks.

Upvotes: 2

Views: 4977

Answers (2)

himanshu
himanshu

Reputation: 2205

You could use request module to do HTTP request (or for calling REST API) painlessly. When response is available then in the callback you can do your assertions. For assertions you can use any assertion module that you like but I would suggest give should.js module a try. It's fun to write the should statements.

Regarding choosing between mocha, jasmine, vows or any other testing framework. It does not matter which one you choose. Look at their API and select the framework you think will be productive for you and matches your thinking and coding style.

Upvotes: 2

Charles
Charles

Reputation: 11778

I personally use api-easy, it's based on vows and request. With this module you can easily test REST APIs.

Here an example:

 var APIeasy = require('api-easy'),
      assert = require('assert');

  var suite = APIeasy.describe('your/awesome/api');

  suite.discuss('When using your awesome API')
       .discuss('and your awesome resource')
       .use('localhost', 8080)
       .setHeader('Content-Type', 'application/json')
       .post({ test: 'data' })
         .expect(200, { ok: true })
         .expect('should respond with x-test-header', function (err, res, body) {
           assert.include(res.headers, 'x-test-header');
         })
       .export(module);

Upvotes: 3

Related Questions