e8f79d43f9
closes #517 add index test for Application add app.toJSON test add test for app.inspect() add tests for app.use() add tests for app.onerror() add tests for app.respond() add tests for app.context() add tests for app.request() add tests for app.response refactor for non-existence of test/app...js no need for *.js use helpers/ dir for non-tests
84 lines
1.6 KiB
JavaScript
84 lines
1.6 KiB
JavaScript
|
|
'use strict';
|
|
|
|
const stderr = require('test-console').stderr;
|
|
const koa = require('../..');
|
|
const AssertionError = require('assert').AssertionError;
|
|
|
|
describe('app.onerror(err)', function(){
|
|
it('should throw an error if a non-error is given', function(done){
|
|
const 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){
|
|
const app = koa();
|
|
const err = new Error();
|
|
|
|
err.status = 404;
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
app.onerror(err);
|
|
});
|
|
|
|
output.should.eql([]);
|
|
|
|
done();
|
|
})
|
|
|
|
it('should do nothing if .silent', function(done){
|
|
const app = koa();
|
|
app.silent = true;
|
|
const err = new Error();
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
app.onerror(err);
|
|
});
|
|
|
|
output.should.eql([]);
|
|
|
|
done();
|
|
})
|
|
|
|
it('should log the error to stderr', function(done){
|
|
const app = koa();
|
|
app.env = 'dev';
|
|
|
|
const err = new Error();
|
|
err.stack = 'Foo';
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
app.onerror(err);
|
|
});
|
|
|
|
output.should.eql(["\n", " Foo\n", "\n"]);
|
|
|
|
done();
|
|
})
|
|
|
|
it('should use err.toString() instad of err.stack', function(done){
|
|
const app = koa();
|
|
app.env = 'dev';
|
|
|
|
const err = new Error('mock stack null');
|
|
err.stack = null;
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
app.onerror(err);
|
|
});
|
|
|
|
output.should.eql(["\n", " Error: mock stack null\n", "\n"]);
|
|
|
|
done();
|
|
})
|
|
})
|