Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
converting over to chai and sinon. Adding tests for custom handlers. C…
Browse files Browse the repository at this point in the history
…loses #32
  • Loading branch information
ekryski committed Apr 3, 2016
1 parent e6458f1 commit 0765dbe
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 28 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@
"babel-plugin-transform-object-assign": "^6.3.13",
"babel-polyfill": "^6.5.0",
"babel-preset-es2015": "^6.3.13",
"chai": "^3.5.0",
"feathers": "^1.2.0",
"jshint": "^2.8.0",
"mocha": "^2.3.4",
"request": "^2.69.0"
"request": "^2.69.0",
"sinon": "^1.17.3",
"sinon-chai": "^2.8.0"
}
}
145 changes: 118 additions & 27 deletions test/error-handler.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,115 @@
/*jshint expr: true, unused: false*/

if(!global._babelPolyfill) { require('babel-polyfill'); }

import feathers from 'feathers';
import assert from 'assert';
import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import request from 'request';
import fs from 'fs';
import { join } from 'path';
import { errors } from '../src';
import handler from '../src/error-handler';

chai.use(sinonChai);

const content = '<html><head></head><body>Error</body></html>';

let htmlHandler = sinon.spy(function(error, req, res, next) {
res.send(content);
});

const jsonHandler = sinon.spy(function(error, req, res, next) {
res.json(error);
});

describe('feathers-errors', () => {
it('is CommonJS compatible', () => {
assert.equal(typeof require('../lib/error-handler'), 'function');
expect(typeof require('../lib/error-handler')).to.equal('function');
});

it('can be required at the root', () => {
assert.equal(typeof require('../handler'), 'function');
expect(typeof require('../handler')).to.equal('function');
});

it('is import compatible', () => {
assert.equal(typeof handler, 'function');
expect(typeof handler).to.equal('function');
});

describe('supports custom handlers', function() {
before(function() {
this.app = feathers()
.get('/error', function(req, res, next) {
next(new Error('Something went wrong'));
})
.use(handler({
html: htmlHandler,
json: jsonHandler
}));

this.server = this.app.listen(5050);
});

after(function(done) {
this.server.close(done);
});

describe('HTML handler', () => {
const options = {
url: 'http://localhost:5050/error',
headers: {
'Content-Type': 'text/html',
'Accept': 'text/html'
}
};

it('is called', done => {
request(options, (error, res, body) => {
expect(htmlHandler).to.be.called;
done();
});
});

it('can send a custom response', done => {
request(options, (error, res, body) => {
expect(body).to.equal(content);
done();
});
});
});

describe('JSON handler', () => {
const options = {
url: 'http://localhost:5050/error',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};

it('is called', done => {
request(options, (error, res, body) => {
expect(jsonHandler).to.be.called;
done();
});
});

it('can send a custom response', done => {
const expected = JSON.stringify({
name: 'GeneralError',
message: 'Something went wrong',
code: 500,
className: 'general-error',
data: {},
errors: {}
});
request(options, (error, res, body) => {
expect(body).to.deep.equal(expected);
done();
});
});
});
});

describe('use as app error handler', function() {
Expand Down Expand Up @@ -61,8 +152,8 @@ describe('feathers-errors', () => {
url: 'http://localhost:5050/error',
json: true
}, (error, res, body) => {
assert.equal(res.statusCode, 500);
assert.deepEqual(body, {
expect(res.statusCode).to.equal(500);
expect(body).to.deep.equal({
name: 'GeneralError',
message: 'Something went wrong',
code: 500,
Expand All @@ -75,7 +166,7 @@ describe('feathers-errors', () => {
});

it.skip('still has a stack trace', () => {
assert.equal(handler, 'function');
expect(handler).to.equal('function');
});
});

Expand All @@ -89,8 +180,8 @@ describe('feathers-errors', () => {
'Accept': 'text/html'
}
}, (error, res, body) => {
assert.equal(res.statusCode, 404);
assert.equal(html.toString(), body);
expect(res.statusCode).to.equal(404);
expect(html.toString()).to.equal(body);
done();
});
});
Expand All @@ -105,8 +196,8 @@ describe('feathers-errors', () => {
'Accept': 'text/html'
}
}, (error, res, body) => {
assert.equal(res.statusCode, 500);
assert.equal(html.toString(), body);
expect(res.statusCode).to.equal(500);
expect(html.toString()).to.equal(body);
done();
});
});
Expand All @@ -120,8 +211,8 @@ describe('feathers-errors', () => {
'Content-Type': 'text/html'
}
}, (error, res, body) => {
assert.equal(res.statusCode, 404);
assert.equal(html.toString(), body);
expect(res.statusCode).to.equal(404);
expect(html.toString()).to.equal(body);
done();
});
});
Expand All @@ -135,8 +226,8 @@ describe('feathers-errors', () => {
'Accept': 'text/html'
}
}, (error, res, body) => {
assert.equal(res.statusCode, 404);
assert.equal(html.toString(), body);
expect(res.statusCode).to.equal(404);
expect(html.toString()).to.equal(body);
done();
});
});
Expand All @@ -153,8 +244,8 @@ describe('feathers-errors', () => {
},
json: true
}, (error, res, body) => {
assert.equal(res.statusCode, 500);
assert.deepEqual(body, {
expect(res.statusCode).to.equal(500);
expect(body).to.deep.equal({
name: 'GeneralError',
message: 'Something went wrong',
code: 500,
Expand All @@ -175,8 +266,8 @@ describe('feathers-errors', () => {
},
json: true
}, (error, res, body) => {
assert.equal(res.statusCode, 404);
assert.deepEqual(body, { name: 'NotFound',
expect(res.statusCode).to.equal(404);
expect(body).to.deep.equal({ name: 'NotFound',
message: 'File not found',
code: 404,
className: 'not-found',
Expand All @@ -195,8 +286,8 @@ describe('feathers-errors', () => {
},
json: true
}, (error, res, body) => {
assert.equal(res.statusCode, 400);
assert.deepEqual(body, { name: 'BadRequest',
expect(res.statusCode).to.equal(400);
expect(body).to.deep.equal({ name: 'BadRequest',
message: 'Invalid Password',
code: 400,
className: 'bad-request',
Expand All @@ -221,8 +312,8 @@ describe('feathers-errors', () => {
},
json: true
}, (error, res, body) => {
assert.equal(res.statusCode, 400);
assert.deepEqual(body, { name: 'BadRequest',
expect(res.statusCode).to.equal(400);
expect(body).to.deep.equal({ name: 'BadRequest',
message: 'Invalid Password',
code: 400,
className: 'bad-request',
Expand All @@ -247,8 +338,8 @@ describe('feathers-errors', () => {
},
json: true
}, (error, res, body) => {
assert.equal(res.statusCode, 400);
assert.deepEqual(body, { name: 'BadRequest',
expect(res.statusCode).to.equal(400);
expect(body).to.deep.equal({ name: 'BadRequest',
message: 'Invalid Password',
code: 400,
className: 'bad-request',
Expand Down Expand Up @@ -283,8 +374,8 @@ describe('feathers-errors', () => {
}]
});

assert.equal(res.statusCode, 400);
assert.deepEqual(body, expected);
expect(res.statusCode).to.equal(400);
expect(body).to.deep.equal(expected);
done();
});
});
Expand Down

0 comments on commit 0765dbe

Please sign in to comment.