4

Given an existing Node.js application that implements a RESTful API with JSON format, what would be good options for writing an integration testing suite in Node.js?

This suite should execute test scenarios that would typically consist of setting a database to a known state (possibly through POST requests) and running a series of tests involving GET, POST, PUT and DELETE requests, checking for returned status codes and responses.

Fernando Correia
  • 21,803
  • 13
  • 83
  • 116

1 Answers1

4

There are a few options for unit testing frameworks.

I'll show examples with expresso & vows.

var assert = require('assert'),
    http = require('http'),
    server = getServer(); // get an instance of the HTTPServer

assert.response(server, {
    'url': '/'
}, {
    'body': "test body",
    'status': "200"
});

And an example with vows-is

var is = require("vows-is");

is.config({
    server: {
        "factory": createServer,
        "kill": destroyServer,
        "uri": "http://localhost:4000"
    }
})

is.suite("integration tests").batch()

    .context("a request to GET /")
    .topic.is.a.request("GET /")
    .vow.it.should.have.status(200)
    .vow.it.should.have
        .header("content-type", "text/html; charset=utf-8")
    .context("contains a body that")
        .topic.is.property('body')
        .vow.it.should.be.ok
        .vow.it.should.include.string("hello world")

.suite().run({
    reporter: is.reporter
}, function() {
    is.end()
});

vows-is is a thin abstraction on top of vows to make it easier to test with.

However vows-is is under active development so use at your own risk.

Raynos
  • 166,823
  • 56
  • 351
  • 396