Created
October 17, 2013 20:30
-
-
Save skw/7031702 to your computer and use it in GitHub Desktop.
example mocha with custom args
This file contains hidden or 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
{ | |
"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": "*" | |
} | |
} |
This file contains hidden or 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
{ | |
"url": "http://dev-catalyst.viso.tv/" | |
} |
This file contains hidden or 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
// deps | |
... | |
// grab options | |
var options = require( './config.json' ); | |
// tests | |
describe('do stuff with config.url', function() { | |
//... | |
}); |
This file contains hidden or 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
/* | |
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