Permalink
Browse files

add unittest and coverage

  • Loading branch information...
1 parent 8ca423c commit f0cca46fd608e02265bc07da2bc4a7a245d68069 @fengmk2 fengmk2 committed Dec 26, 2012
Showing with 160 additions and 5 deletions.
  1. +16 −0 .gitignore
  2. +4 −0 .travis.yml
  3. +23 −0 Makefile
  4. +1 −1 index.js
  5. +2 −4 lib/connect-domain.js
  6. +8 −0 package.json
  7. +106 −0 test/domain.test.js
View
@@ -0,0 +1,16 @@
+lib-cov
+coverage.html
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
View
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.9
+ - 0.8
View
@@ -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
View
@@ -1 +1 @@
-module.exports = require('./lib/connect-domain');
+module.exports = process.env.CONNECT_DOMAIN_COV ? require('./lib-cov/connect-domain') : require('./lib/connect-domain');
View
@@ -3,7 +3,7 @@
var domain = require('domain');
module.exports = function (handler) {
- return function (req, res, next) {
+ return function domainMiddleware(req, res, next) {
var reqDomain = domain.create();
res.on('close', function () {
@@ -18,9 +18,7 @@
}
});
- reqDomain.run(function () {
- next();
- });
+ reqDomain.run(next);
};
};
}());
View
@@ -9,5 +9,13 @@
"type": "git",
"url": "git://github.com/baryshev/connect-domain.git"
},
+ "devDependencies": {
+ "should": "*",
+ "connect": "*",
+ "jscover": "*",
+ "supertest": "*",
+ "pedding": "*",
+ "mocha": "*"
+ },
"engines" : { "node": ">= 0.8.0" }
}
View
@@ -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.