Skip to content

Instantly share code, notes, and snippets.

@tommystanton
Last active December 23, 2015 05:58
Show Gist options
  • Select an option

  • Save tommystanton/6590337 to your computer and use it in GitHub Desktop.

Select an option

Save tommystanton/6590337 to your computer and use it in GitHub Desktop.
Learning TDD in Node.js using Mocha and should (and journey, to simulate a web application).
$ npm install mocha should journey
$ node_modules/.bin/mocha --ui tdd --reporter tap server.checkHello.test.js
1..1
ok 1 Response from /hello URI should return JSON containing "hello"
# tests 1
# pass 1
# fail 0
var server = require('./server'),
request = require('request'),
should = require('should'),
util = require('util');
var port = 9090,
testUrl = util.format("http://localhost:%d", port),
s = server.createServer(port);
suite("Response from /hello URI", function () {
var url = testUrl + "/hello";
test("should return JSON containing \"hello\"", function (done) {
request(url, function(err, res, body) {
res.should.have.status(200).and.be.json;
res.body.should.match(/hello/);
done();
});
});
});
var http = require('http'),
journey = require('journey');
var router = new(journey.Router);
router.map(function () {
this.get('/hello').bind(function (request, response, data) {
response.send(200, {}, { 'hello': 'world' });
});
});
exports.createServer = function(port) {
var s = http.createServer(function (request, response) {
var body = '';
request.addListener('data', function (chunk) { body += chunk; });
request.addListener('end', function() {
router.handle(request, body, function (result) {
response.writeHead(result.status, result.headers);
response.end(result.body);
});
});
});
s.port = port;
//s.url = 'http://localhost:' + port;
s.listen(s.port);
return s;
};
@tommystanton
Copy link
Author

I mentioned this Gist in a comment on Stack Overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment