Skip to content

Commit

Permalink
add unittest and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 26, 2012
1 parent 8ca423c commit f0cca46
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 5 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,16 @@
lib-cov
coverage.html
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.9
- 0.8
23 changes: 23 additions & 0 deletions Makefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,23 @@
TESTS = test/*.test.js
REPORTER = spec
TIMEOUT = 1000
JSCOVERAGE = ./node_modules/jscover/bin/jscover

install:
@npm install

test: install
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
$(TESTS)

test-cov: lib-cov
@CONNECT_DOMAIN_COV=1 $(MAKE) test
@CONNECT_DOMAIN_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html

lib-cov: install
@rm -rf $@
@$(JSCOVERAGE) lib $@

.PHONY: test-cov test lib-cov
2 changes: 1 addition & 1 deletion index.js
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/connect-domain'); module.exports = process.env.CONNECT_DOMAIN_COV ? require('./lib-cov/connect-domain') : require('./lib/connect-domain');
6 changes: 2 additions & 4 deletions lib/connect-domain.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var domain = require('domain'); var domain = require('domain');


module.exports = function (handler) { module.exports = function (handler) {
return function (req, res, next) { return function domainMiddleware(req, res, next) {
var reqDomain = domain.create(); var reqDomain = domain.create();


res.on('close', function () { res.on('close', function () {
Expand All @@ -18,9 +18,7 @@
} }
}); });


reqDomain.run(function () { reqDomain.run(next);
next();
});
}; };
}; };
}()); }());
8 changes: 8 additions & 0 deletions package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@
"type": "git", "type": "git",
"url": "git://github.com/baryshev/connect-domain.git" "url": "git://github.com/baryshev/connect-domain.git"
}, },
"devDependencies": {
"should": "*",
"connect": "*",
"jscover": "*",
"supertest": "*",
"pedding": "*",
"mocha": "*"
},
"engines" : { "node": ">= 0.8.0" } "engines" : { "node": ">= 0.8.0" }
} }
106 changes: 106 additions & 0 deletions test/domain.test.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,106 @@
/*!
* connect-domain - test/domian.test.js
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var connect = require('connect');
var connectDomain = require('../');
var should = require('should');
var request = require('supertest');
var pedding = require('pedding');


describe('domain.test.js', function () {
var normalHandler = function normalHandler(req, res, next) {
if (req.url === '/sync_error') {
throw new Error('sync_error');
}
if (req.url === '/async_error') {
process.nextTick(function () {
ff.foo();
});
return;
}
res.end(req.url);
};

var errorHandler = function errorHandler(err, req, res, next) {
res.statusCode = 500;
res.end(err.message);
};

var app = connect()
.use(connectDomain())
.use(normalHandler)
.use(errorHandler);

var app2 = connect()
.use(connectDomain(function errorHandler2(err, req, res, next) {
err.message += ', process by errorHandler2';
next(err);
}))
.use(normalHandler)
.use(errorHandler);

it('should GET / status 200', function (done) {
done = pedding(2, done);

request(app)
.get('/')
.expect(200, done);

request(app2)
.get('/')
.expect(200, done);
});

it('should GET /sync_error status 500', function (done) {
done = pedding(2, done);

request(app)
.get('/sync_error')
.expect('sync_error')
.expect(500, done);

request(app2)
.get('/sync_error')
.expect('sync_error')
.expect(500, done);
});

describe('hack for async error', function () {
// Because `domain` will still throw `uncaughtException`, we need to hack for `mocha` test.
// https://github.com/joyent/node/issues/4375
// https://gist.github.com/4179636
var mochaHandler;
before(function () {
mochaHandler = process.listeners('uncaughtException').pop();
});
after(function () {
// ...but be sure to re-enable mocha's error handler
process.on('uncaughtException', mochaHandler);
});

it('should GET /async_error status 500', function (done) {
done = pedding(2, done);

request(app)
.get('/async_error')
.expect('ff is not defined')
.expect(500, done);

request(app2)
.get('/async_error')
.expect('ff is not defined, process by errorHandler2')
.expect(500, done);
});
});

});

0 comments on commit f0cca46

Please sign in to comment.