koa-lite/test/application/onerror.js

74 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
2017-05-11 03:30:32 +00:00
const assert = require('assert');
2015-10-13 06:19:42 +00:00
const Koa = require('../..');
const mm = require('mm');
describe('app.onerror(err)', () => {
afterEach(mm.restore);
2017-05-11 03:30:32 +00:00
it('should throw an error if a non-error is given', () => {
const app = new Koa();
2017-05-11 03:30:32 +00:00
assert.throws(() => {
app.onerror('foo');
}, TypeError, 'non-error thrown: foo');
2015-10-12 20:36:41 +00:00
});
2017-05-11 03:30:32 +00:00
it('should do nothing if status is 404', () => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
const err = new Error();
err.status = 404;
let called = false;
mm(console, 'error', () => { called = true; });
2017-05-11 03:30:32 +00:00
app.onerror(err);
assert(!called);
2015-10-12 20:36:41 +00:00
});
2017-05-11 03:30:32 +00:00
it('should do nothing if .silent', () => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.silent = true;
const err = new Error();
let called = false;
mm(console, 'error', () => { called = true; });
2017-05-11 03:30:32 +00:00
app.onerror(err);
assert(!called);
2015-10-12 20:36:41 +00:00
});
2017-05-11 03:30:32 +00:00
it('should log the error to stderr', () => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.env = 'dev';
const err = new Error();
err.stack = 'Foo';
let msg = '';
mm(console, 'error', input => {
if (input) msg = input;
});
2017-05-11 03:30:32 +00:00
app.onerror(err);
assert(msg === ' Foo');
2015-10-12 20:36:41 +00:00
});
2017-05-11 03:30:32 +00:00
it('should use err.toString() instad of err.stack', () => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.env = 'dev';
const err = new Error('mock stack null');
err.stack = null;
2017-05-11 03:30:32 +00:00
app.onerror(err);
let msg = '';
mm(console, 'error', input => {
if (input) msg = input;
});
app.onerror(err);
assert(msg === ' Error: mock stack null');
2015-10-12 20:36:41 +00:00
});
});