Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mocha, Chai and Supertest combo for testing #29

Merged
merged 2 commits into from
Nov 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/routes/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ router.route('/')

// get the status (accessed at GET http://localhost:8080/status)
.get(function(req, res) {
var stat = {"status":"OK"};
var stat = {"status":"running"}
res.send(stat);
});

Expand Down
13 changes: 10 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/* File: gulpfile.js */

// grab our packages
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var nodemon = require('gulp-nodemon');
var mocha = require('gulp-mocha');

// define the default task and add the watch task to it
gulp.task('default', ['watch']);

// configure the jshint task
gulp.task('jshint', function() {
gulp.task('lint', function() {
return gulp.src('app/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
Expand All @@ -23,3 +24,9 @@ gulp.task('watch', function() {
// ./build/ directory changes
nodemon({script: 'server.js', watch: 'app/**'});
});

gulp.task('test', function() {
return gulp
.src('test/*.js')
.pipe(mocha());
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"url": "https://github.com/Mainflux/mainflux"
},
"license": "Apache-2.0",

"dependencies": {
"express": "~4.0.0",
"mongoose": "~3.6.13",
Expand All @@ -17,8 +16,11 @@
"lodash": "~3.10.1"
},
"devDependencies": {
"chai": "^3.4.0",
"gulp-jshint": "^1.11.2",
"gulp-mocha": "^2.1.3",
"gulp-nodemon": "^2.0.3",
"jshint-stylish": "^2.0.1"
"jshint-stylish": "^2.0.1",
"supertest": "^1.1.0"
}
}
45 changes: 30 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// server.js
/**
* Copyright (c) Mainflux
*
* Mainflux server is licensed under an Apache license, version 2.0 license.
* All rights not explicitly granted in the Apache license, version 2.0 are reserved.
* See the included LICENSE file for more details.
*/

/**
* Extrenal configs are kept in the config.js file on the same level
Expand All @@ -7,39 +13,48 @@ var config = require('./config/config');
console.log(config.message);


// BASE SETUP
// =============================================================================

// call the packages we need
/**
* SETUP
*/
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');

// MongoDB
/** MongoDB */
var mongoose = require('mongoose');
// Docker MongoDB url
var docker_mongo_url = process.env.MAINFLUX_MONGODB_1_PORT_27017_TCP_ADDR

mongoose.connect(docker_mongo_url || config.db.path + ':' + config.db.port + '/' + config.db.name); // connect to our database
/** Docker MongoDB url */
var docker_mongo_url = process.env.MAINFLUX_MONGODB_1_PORT_27017_TCP_ADDR

/** Connect to DB */
mongoose.connect(docker_mongo_url || config.db.path + ':' + config.db.port + '/' + config.db.name);

// configure app to use bodyParser()
// this will let us get the data from a POST
/** Configure app to use bodyParser() */
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || config.port; // set our port

// ROUTES FOR OUR API
// =============================================================================

/**
* ROUTES
*/
app.use('/status', require('./app/routes/status'));
app.use('/devices', require('./app/routes/devices'));
app.use('/users', require('./app/routes/users'));
app.use('/sessions', require('./app/routes/sessions'));



// START THE SERVER
// =============================================================================

/**
* SERVER START
*/
app.listen(port);
console.log('Magic happens on port ' + port);


/**
* Export app for testing
*/
module.exports = app;
35 changes: 35 additions & 0 deletions test/apiTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** Chai stuff */
var should = require('chai').should;
var expect = require('chai').expect;

/** Supertest for API */
var supertest = require('supertest');
var server = require('../server');
var api = supertest(server);

/**
* API test description
*/
describe('loading express', function () {
/**
* /status
*/
it('responds to /status', function testSlash(done) {
api
.get('/status')
.expect(200)
.end(function(err, res){
expect(res.body.status).to.equal("running");
done();
});
});

/**
* /foo/bar
*/
it('404 /foo/bar', function testPath(done) {
api
.get('/foo/bar')
.expect(404, done);
});
});