From a85f5809837612034759928dc322e717d9752420 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Mon, 8 Sep 2014 23:53:46 +0100 Subject: [PATCH] Add full coverage --- package.json | 9 +++--- test/application.js | 63 +++++++++++++++++++++++++++++++++++++++++ test/context/inspect.js | 11 +++++++ test/response/socket.js | 10 +++++++ 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 test/context/inspect.js create mode 100644 test/response/socket.js diff --git a/package.json b/package.json index 3b56304..7d43920 100644 --- a/package.json +++ b/package.json @@ -39,11 +39,12 @@ "only": "0.0.2" }, "devDependencies": { - "should": "^3.1.0", - "mocha": "^1.17.0", - "supertest": "~0.13.0", "istanbul-harmony": "~0.3.0", - "make-lint": "^1.0.1" + "make-lint": "^1.0.1", + "mocha": "^1.17.0", + "should": "^3.1.0", + "supertest": "~0.13.0", + "test-console": "^0.7.1" }, "engines": { "node": ">= 0.11.13" diff --git a/test/application.js b/test/application.js index 3c4d35c..f8ad932 100644 --- a/test/application.js +++ b/test/application.js @@ -4,6 +4,8 @@ var assert = require('assert'); var http = require('http'); var koa = require('..'); var fs = require('fs'); +var stderr = require("test-console").stderr; +var AssertionError = assert.AssertionError; describe('app', function(){ it('should handle socket errors', function(done){ @@ -114,6 +116,67 @@ describe('app.use(fn)', function(){ }) }) +describe('app.onerror(err)', function(){ + it('should throw an error if a non-error is given', function(done){ + var app = koa(); + + try { + app.onerror('foo'); + + should.fail(); + } catch (err) { + err.should.be.instanceOf(AssertionError); + err.message.should.equal('non-error thrown: foo'); + } + + done(); + }) + + it('should do nothing if status is 404', function(done){ + var app = koa(); + var err = new Error(); + + err.status = 404; + + var output = stderr.inspectSync(function() { + app.onerror(err); + }); + + output.should.eql([]); + + done(); + }) + + it('should do nothing if env is test', function(done){ + var app = koa(); + var err = new Error(); + + var output = stderr.inspectSync(function() { + app.onerror(err); + }); + + output.should.eql([]); + + done(); + }) + + it('should log the error to stderr', function(done){ + var app = koa(); + app.env = 'dev'; + + var err = new Error(); + err.stack = 'Foo'; + + var output = stderr.inspectSync(function() { + app.onerror(err); + }); + + output.should.eql(["\n", " Foo\n", "\n"]); + + done(); + }) +}) + describe('app.respond', function(){ describe('when this.respond === false', function(){ it('should bypass app.respond', function(done){ diff --git a/test/context/inspect.js b/test/context/inspect.js new file mode 100644 index 0000000..8af6ca6 --- /dev/null +++ b/test/context/inspect.js @@ -0,0 +1,11 @@ + +var context = require('../context'); + +describe('ctx.inspect()', function(){ + it('should return a json representation', function(){ + var ctx = context(); + var toJSON = ctx.toJSON(ctx); + + toJSON.should.eql(ctx.inspect()); + }) +}) diff --git a/test/response/socket.js b/test/response/socket.js new file mode 100644 index 0000000..3b38623 --- /dev/null +++ b/test/response/socket.js @@ -0,0 +1,10 @@ + +var Stream = require('stream'); +var response = require('../context').response; + +describe('res.socket', function(){ + it('should return the request socket object', function(){ + var res = response(); + res.socket.should.be.instanceOf(Stream); + }) +})