Created
July 30, 2014 03:01
-
-
Save lesterzone/29f2f44cc87e82e82ca4 to your computer and use it in GitHub Desktop.
My approach to test GeddyJS(an API application) with Mocha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SomeNamespace.validModel = function(modelName, inf) { | |
var model = geddy.model[modelName].create(inf); | |
describe('Valid Model', function() { | |
it('should be valid with all required properties', function() { | |
if (!model.isValid()) { | |
console.log("\t", model.errors); | |
} | |
expect(model.isValid()).to.be.equal(true); | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Namespace to run some componnents of the API | |
* @ref http://jebchit.info/pr/browse.php/Oi8vamFr/ZWpzLmNv/bS9kb2Nz/b5/ | |
* @return {Object} | |
*/ | |
namespace('tests', function() { | |
var Mocha = require('mocha'), | |
fs = require('fs'), | |
mocha, runTests; | |
mocha = new Mocha({ | |
reporter: 'spec', | |
ui: 'bdd', | |
timeout: '10000' | |
}); | |
/** | |
* Function reused to run tests from models and controllers tasks. | |
* @return {Function} | |
*/ | |
runTests = function() { | |
mocha.options.ignoreLeaks = true; | |
mocha.run(); | |
}; | |
/** | |
* Helpers to be reused on similar tests | |
*/ | |
mocha.addFile('test/config.js'); | |
/** | |
* Do not require chai and set chai's properties to scope variables on any | |
* test. | |
* global.should = chai.should(); | |
* global.assert = chai.assert; | |
*/ | |
global.chai = require('chai'); | |
/** | |
* TODO: research about assigning this as global is bad or acceptable | |
*/ | |
global.expect = chai.expect; | |
desc('With this task, we will run all tests related to models'); | |
task('models', function(model) { | |
fs.readdirSync('test/models').filter(function(file) { | |
/** | |
* Add all files from 'test/models' directory if no model was passed | |
* as param | |
* CLI: geddy jake tests:models | |
*/ | |
if (!model) { | |
return mocha.addFile('test/models/' + file); | |
} | |
/** | |
* Add only model passed as param | |
* CLI: geddy jake tests:models[modelName] | |
*/ | |
if (file.toLowerCase().replace('.js', '') === model) { | |
return mocha.addFile('test/models/' + file); | |
} | |
}); | |
return runTests(); | |
}); | |
desc('With this task, we will run all tests related to controllers'); | |
task('controllers', function(controller) { | |
fs.readdirSync('test/controllers').filter(function(file) { | |
/** | |
* Add all files from test/controllers folder, if there's no | |
* controller from params | |
* CLI; | |
* geddy jake tests:controllers | |
*/ | |
if (!controller) { | |
return mocha.addFile('test/controllers/' + file); | |
} | |
/** | |
* Add only controller passed as params | |
* CLI: | |
* geddy jake tests:controllers[controllerName] | |
*/ | |
if (file.toLowerCase().replace('.js', '') === controller) { | |
return mocha.addFile('test/controllers/' + file); | |
} | |
}); | |
return runTests(); | |
}); | |
}); | |
task('test', function(done) { | |
jake.Task['tests:models'].invoke(); | |
jake.Task['tests:controllers'].invoke(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"devDependencies": { | |
"chai": "1.9.1", | |
"mocha": "1.21.0", | |
"node-inspector": "^0.7.4", | |
"request": "^2.34.0" | |
} |
TODO:
- I'll like to run test on file change, i.e: if ModelX is modified, run tests from /tests/models/ModelX
Looks pretty reasonable. I'm just using Jake's built-in TestTask for testing, but I can certainly see the appeal of something like Mocha that's ubiquitous and well supported.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: