Skip to content

Instantly share code, notes, and snippets.

@skw
Created October 17, 2013 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skw/7031702 to your computer and use it in GitHub Desktop.
Save skw/7031702 to your computer and use it in GitHub Desktop.
example mocha with custom args
{
"name": "test"
, "version": "0.0.1"
, "description": "mocha"
, "keywords": ["test", "tutorial"]
, "author": "skw"
, "main": "index"
, "engines": { "node": "*" }
, "scripts": {
"test": "make test"
}
, "devDependencies": {
"mocha": "*",
"optimist": "*",
"xunit-file": "*",
"lodash": "*"
}
}
{
"url": "http://dev-catalyst.viso.tv/"
}
// deps
...
// grab options
var options = require( './config.json' );
// tests
describe('do stuff with config.url', function() {
//...
});
/*
example mocha with custom args
// pass url with
// ``` $ test --url http://dev-catalyst.viso.tv/ ```
see: https://github.com/visionmedia/mocha/wiki/Using-mocha-programmatically
*/
var Mocha = require('mocha'),
fs = require('fs'),
path = require('path'),
optimist = require( 'optimist' ),
_ = require( 'lodash' ),
defaults;
// check default config
if( fs.existsSync( __dirname + '/test/_config.json' )){
defaults = require( __dirname + 'test/_config.json' );
} else {
process.exit( 1 );
}
var options = _.clone( defaults );
// check for arg
if( typeof optimist.argv.url !== 'undefined' ){
options.url = optimist.argv.url;
}
// write file config.json
fs.writeFileSync(
__dirname + '/test/config.json',
JSON.stringify( options ) //think this needs to be parsed but require auto parses
);
// do mocha stuff, could drop options into json file or args
var mocha = new Mocha({
// options
ui: 'bdd'
});
fs.readdirSync( __dirname + '/test' ).filter( function( file ){
// Only keep the .js files
return file.substr( -3 ) === '.js';
}).forEach( function( file ){
// Use the method "addFile" to add the file to mocha
mocha.addFile(
path.join( __dirname + '/test', file )
);
});
// Now, you can run the tests.
mocha.run( function( failures ){
process.on( 'exit', function () {
process.exit( failures );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment