Skip to content
This repository has been archived by the owner on Jun 2, 2021. It is now read-only.

Commit

Permalink
add coverage test
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jun 1, 2015
1 parent 6b7f030 commit 61d7640
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_js:
- "0.12"
- "iojs"
sudo: false
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
17 changes: 13 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
'use strict'

var gulp = require('gulp')
var gulpSequence = require('gulp-sequence')
var merge = require('merge2')
var mocha = require('gulp-mocha')
var istanbul = require('gulp-istanbul')
var gulpSequence = require('gulp-sequence')

gulp.task('mocha', function () {
return gulp.src(['test/index.js', 'test/context/*', 'test/request/*', 'test/response/*'], {read: false})
.pipe(mocha())
gulp.task('mocha', function (done) {
return merge(
gulp.src(['index.js', 'lib/*.js'])
.pipe(istanbul()) // Covering files
.pipe(istanbul.hookRequire()), // Force `require` to return covered files
gulp.src(['test/index.js', 'test/context/*', 'test/request/*', 'test/response/*'])
.pipe(mocha({timeout: 10000}))
.pipe(istanbul.writeReports()) // Creating the reports after tests runned
.pipe(istanbul.enforceThresholds({thresholds: {global: 90}})) // Enforce a coverage of at least 90%
)
})

gulp.task('default', ['test'])
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var pwdReg = new RegExp(process.cwd().replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g
module.exports = Toa

Toa.NAME = 'Toa'
Toa.VERSION = 'v0.11.0'
Toa.VERSION = 'v0.11.1'

function Toa (server, body, options) {
if (!(this instanceof Toa)) return new Toa(server, body, options)
Expand Down
7 changes: 3 additions & 4 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ proto.inspect = function () {
*/

proto.toJSON = function () {
var context = {
return {
request: this.request.toJSON(),
response: this.response.toJSON(),
config: this.config,
originalUrl: this.originalUrl,
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
}
if (this.request) context.request = this.request.toJSON()
if (this.response) context.response = this.response.toJSON()
return context
}

/**
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Yan Qing <admin@zensh.com>"
],
"version": "0.11.0",
"version": "0.11.1",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -49,8 +49,10 @@
},
"devDependencies": {
"gulp": "^3.8.11",
"gulp-istanbul": "^0.9.0",
"gulp-mocha": "^2.1.0",
"gulp-sequence": "^0.3.2",
"merge2": "^0.3.5",
"standard": "^4.0.0",
"supertest": "^1.0.1",
"test-console": "^0.7.1"
Expand Down
140 changes: 140 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var stderr = require('test-console').stderr
var request = require('supertest')
var statuses = require('statuses')
var assert = require('assert')
var http = require('http')
var toa = require('..')
var fs = require('fs')

Expand All @@ -28,9 +29,126 @@ describe('app', function () {
.get('/')
.end(function () {})
})

it('should work with custom server', function (done) {
var server = http.createServer()
var app = toa(server, function () {
this.body = 'hello'
})

assert.strictEqual(app.server, server)

request(app.listen())
.get('/')
.expect('hello')
.end(done)
})

it('should work with error handle', function (done) {
var app = toa(function () {
this.throw(404)
}, function (err) {
this.body = 'hello'
assert.strictEqual(err.status, 404)
return true
})

request(app.listen())
.get('/')
.expect('hello')
.end(done)
})

it('should throw errorHandle\'s error', function (done) {
var app = toa(function () {
this.throw(404)
}, function (err) {
if (err) throw new Error('errorHandle error')
})

var handleErr = null

app.onerror = function (error) {
handleErr = error
}

request(app.listen())
.get('/')
.expect(500)
.end(function (err) {
assert.strictEqual(handleErr.message, 'errorHandle error')
done(err)
})
})

it('should work with error handle', function (done) {
var app = toa(function () {
this.throw(404)
}, function (err) {
this.body = 'hello'
assert.strictEqual(err.status, 404)
return true
})

request(app.listen())
.get('/')
.expect('hello')
.end(done)
})

it('should respond non-error by onResError', function (done) {
var app = toa(function () {
this.body = 123
throw {
message: 'some message',
status: 206
}
})

request(app.listen())
.get('/')
.expect(206)
.expect({
message: 'some message',
status: 206
})
.end(done)
})

it('should work with options', function (done) {
var debugLogs = 0
var app = toa(function () {
this.throw(404)
}, {
onerror: function (err) {
this.body = 'hello'
assert.strictEqual(err.status, 404)
},
debug: function (err, res) {
debugLogs += 1
if (err) assert.strictEqual(err.status, 404)
}
})

request(app.listen())
.get('/')
.expect(404)
.end(function (err) {
assert.strictEqual(debugLogs > 1, true)
done(err)
})
})
})

describe('app.use(fn)', function () {
it('should throw error with non-function middleware', function (done) {
var app = toa()
assert.throws(function () {
app.use({})
})
done()
})

it('should run middleware befor body', function (done) {
var app = toa(function () {
calls.push(3)
Expand Down Expand Up @@ -89,6 +207,18 @@ describe('app.onerror(err)', function () {
assert.deepEqual(output, [' Foo\n'])
done()
})

it('should transform non-error to error object', function (done) {
var app = toa()

var err = 'Foo'
var output = stderr.inspectSync(function () {
app.onerror(err)
})

assert.strictEqual(output[0].indexOf(' Error: non-error thrown: Foo\n'), 0)
done()
})
})

describe('app.respond', function () {
Expand Down Expand Up @@ -785,6 +915,16 @@ describe('app.context', function () {
.expect(204, done)
})

it('should throw error with non-object config', function (done) {
var app = toa()

assert.throws(function () {
app.config = []
})
assert.strictEqual(app.config.poweredBy, 'Toa')
done()
})

it('should not affect the application config', function (done) {
var app = toa(function () {
assert(this.config.test === 'config')
Expand Down

0 comments on commit 61d7640

Please sign in to comment.