Mocha: Running mocha tests from inside node?

Created on 11 Dec 2011  路  17Comments  路  Source: mochajs/mocha

I've got a whole tree of test files to run and want to be able select which ones to run using some console commands. I tried using a sort of bootstrap file that mocha would load and it would load the other files depending one what console commands I pass through mocha, but apparently commander just complains when you use a command that hasn't been defined.

What I'd really like is to be able to just require mocha in my code and run tests from there. At the moment I'm just using child_process.spawn and piping it's output to the console, which is kind of hacky and doesn't have the pretty colors. :(

Most helpful comment

This old thread is the top Google result, instead of the real answer:

https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically

All 17 comments

when I have a moment I'll document the js api so you can do this sort of thing

Nice. Really enjoying Mocha so far, it's awesome. :)

thanks!

+1 for js api documentation

This would be great. In the meantime, here's an evil little hack I used to get mocha to run from my Cakefile:

process.argv = [ 'mocha' ]
require("mocha/bin/_mocha")

@iangreenleaf you could just use node's exec() or spawn(), or a much smaller makefile :p

Yeah, but like the OP said... pretty colors :)

If it was just to invoke the tests, I'd use a makefile, but our app has other tasks where easy access to the node environment is very helpful, and I want to keep it all in one place.

+1 for that. I am using several other test harnesses for now, would love to be able to incorporate Mocha, the primary reason I cannot is the difficulty of running from an API. Would like to see something like:

var mocha = require('mocha');
mocha.interface('tdd').reporter('dot').load(object1,object2,object3).run();

Really simple to run, would be very powerful: load the interface, indicate the reporter, add an object, run().

265

@deitch yup it wont be difficult, it's already done for the client-side stuff I'll just have to move a bit of the mocha(1) stuff into ./lib and document it

@visionmedia great! WIll you post it here and/or in the nodejs Google group when it is done? Love to be able to start using Mocha in my projects.

Kudos.

Mind if I drop you a direct email 'bout life and business in Canada?

Has anything been done on this?

@leeolayvar not yet

Is there any documentation, anywhere, on setting up a mocha test suite from the (low-level) api?

@leeolayvar nope, just https://github.com/visionmedia/mocha/blob/master/bin/_mocha

some of that can be abstracted back into the library, though personally im not a fan of having glob-style stuff in the lib itself since that's not really a library concern, it should just accept paths, how it gets those paths is a higher-level issue. --watch and things like that probably wont be in the library portion, that doesn't really make sense to me

This old thread is the top Google result, instead of the real answer:

https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically

+1 @evan-king you are a gentleman and a scholar.

Was this page helpful?
0 / 5 - 0 ratings