Skip to content

Instantly share code, notes, and snippets.

@beeekind
Created September 25, 2015 18:01
Show Gist options
  • Save beeekind/7e7cb69913dc1296d580 to your computer and use it in GitHub Desktop.
Save beeekind/7e7cb69913dc1296d580 to your computer and use it in GitHub Desktop.
Example of using async module to setup before hook eloquently for mocha.js tests
"use strict";
var expect = require("expect.js");
var async = require("async");
var superagent = require("superagent");
var api = require("../lib/api");
var serverFactory = require("./helpers/server").init;
var Orm = require("@bjones6/hs-models");
var databaseSettings = { // this is running unexposed on localhost don't get excited
"database": "circle_test",
"username": "ubuntu",
"password": "password",
"dialect": "postgres",
"logging": false
};
describe("Basic usage", function(){
var models;
var server;
before(function(done){
// set to outer scope the result of two asynchronous actions
async.parallel({
models: function(cb){
// Initialize the ORM / database connection
models = Orm(databaseSettings).models;
models.sequelize.sync()
.then(function(){
cb(null, models);
})
.catch(function(err){
cb(err);
});
},
server: function(cb){
// start up a basic test server with our api
serverFactory(api, cb);
}
}, function(err, response){
expect(err).to.eql(null);
models = response.models;
server = response.server;
done();
});
});
it('has successfully utilized to @bjones6/hs-models library and synced the database models', function(done){
expect(models).to.have.keys(['User', 'Account', 'FacebookProfile', 'ContactProfile', 'Connection']);
done();
});
it('has successfully started up a server instance with our modules API', function(done){
// ping our API's ping route
superagent.get("localhost:4200/ping")
.end(function(e, res){
expect(res.status).to.eql(200);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment