Skip to content

Commit

Permalink
test(api): ensure nice logs for api test failures
Browse files Browse the repository at this point in the history
This commit makes the api tests log their failures in a more explicit
manner, rather than relying on the programmer to write the proper text
in to the `it()` statement.  Hopefully this will save time in future bug
hunts.
  • Loading branch information
Jonathan Niles committed Jul 7, 2016
1 parent 6afc01d commit c4c89e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
14 changes: 8 additions & 6 deletions server/test/api/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ helpers.configure(chai);
/*
* The /exchange API endpoint
*/
describe('The /exchange API endpoint', function () {
describe('(/exchange) The /exchange API endpoint', function () {
const agent = chai.request.agent(helpers.baseUrl);
before(helpers.login(agent));

// constants
const RATE = {
enterprise_id :1, // Enterprise ID
currency_id :1, // FC in test database
rate :930,
date :new Date('2015-10-10')
enterprise_id: 1, // Enterprise ID
currency_id : 1, // FC in test database
rate : 930,
date : new Date('2015-10-10')
};

const RATE_KEY = ['id', 'enterprise_id', 'currency_id', 'enterprise_currency_id', 'rate', 'date'];
const RATE_KEY = [
'id', 'enterprise_id', 'currency_id', 'enterprise_currency_id', 'rate', 'date'
];

it('GET /exchange returns a list of exchange rates', function () {
return agent.get('/exchange')
Expand Down
19 changes: 9 additions & 10 deletions server/test/api/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* jshint expr: true */

'use strict';

// import plugins
Expand Down Expand Up @@ -92,24 +91,24 @@ exports.identical = function identical(objectA, objectB) {
* .catch(helpers.handler);
*/
api.created = function created(res) {

// make sure the response has correct HTTP headers
expect(res).to.have.status(201);
expect(res).to.be.json;

// ensure that we received a correct uuid in return
expect(res.body).to.not.be.empty;
expect(res.body, `${res.req.method} ${res.req.path} returned an empty body.`).to.not.be.empty;

// make sure that we either have a UUID or an ID
expect(res.body).to.satisfy(function (o) { return o.id || o.uuid; });
expect(res.body, `${res.req.method} ${res.req.path} did not return an id or uuid.`).to.satisfy(o => o.id || o.uuid);

// id checks
if (res.body.id) {
expect(res.body).to.have.property('id');
expect(res.body.id).to.be.a('number');
expect(res.body.id, `${res.req.method} ${res.req.path} returned a non-numeric id.`).to.be.a('number');

// uuid checks
} else {
expect(res.body).to.have.property('uuid');
expect(res.body, `${res.req.method} ${res.req.path} returned an invalid uuid.`).to.have.property('uuid');
expect(res.body.uuid).to.be.a('string');
expect(res.body.uuid).to.have.length(36);
}
Expand Down Expand Up @@ -206,7 +205,7 @@ api.updated = function updated(res, original, changedKeys) {
api.deleted = function deleted(res) {
// make sure that the response has the correct HTTP headers
expect(res).to.have.status(204);
expect(res.body).to.be.empty;
expect(res.body, `${res.req.method} ${res.req.path} was not empty.`).to.be.empty;
};

/**
Expand All @@ -229,11 +228,11 @@ api.deleted = function deleted(res) {
*/
api.listed = function listed(res, len) {
// make sure that the response has the correct HTTP headers
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res, `${res.req.method} ${res.req.path} returned ${res.res.statusCode} ${res.res.statusMesage}.`).to.have.status(200);
expect(res, `${res.req.method} ${res.req.path} did not return JSON.`).to.be.json;

// assert that the length is the expected length.
expect(res.body).to.have.length(len);
expect(res.body, `${res.req.method} ${res.req.path} did not return an array of length ${len}.`).to.have.length(len);
};

/*
Expand Down

0 comments on commit c4c89e8

Please sign in to comment.