Skip to content

Instantly share code, notes, and snippets.

@trek
Created July 22, 2012 02:40
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trek/3158014 to your computer and use it in GitHub Desktop.
Save trek/3158014 to your computer and use it in GitHub Desktop.
Run your mocha unit tests suite via casper.js
// get a Casper object.
// See http://casperjs.org/
var casper = require('casper').create();
// this will be evaluated inside the context of the window.
// See http://casperjs.org/api.html#casper.evaluate for notes on
// the difference between casper's running environment and the
// DOM environment of the loaded page.
function testReporter(){
// casper is webkit, so we have good DOM methods. You're
// probably unfamiliar with them because of shitty DOMs
// in shitty browsers. `test` is the class applied
// to passing tests in mocha. If you're using a different
// runner, you'll need to figure out how to extract this
// this kind of data from the report.
var testNodes = document.querySelectorAll('.test');
// testNodes is a NodeList so has no useful array methods,
// but we can apply them, I'm returning an object that will
// be used later:
// {
// didPass: true
// text: 'The assertion of this test'
// }
return Array.prototype.map.call(testNodes, function(aTest){
// in mocha, a passed test has a class `pass`. Normally a
// match will return an array of matches, which is `!!`ed
// into an appropriate boolean.
// the assertion of the test is stored in an `h2`
return {
didPass: !!aTest.getAttribute('class').match(/pass/),
text: aTest.querySelector('h2').innerHTML
}
})
};
// open the casper's browser and load a url. This url should be
// where ever your unit tests are being displayed.
casper.start('http://localhost:9292/unit.html');
// casper.then is a promise. It ensures specific execution order.
// give 'javascript promise' a google for some reading.
casper.then(function(){
// this.evaluate runs the specified function inside the context
// of the page that casper is accessing. Think of it like popping
// open the console in Firebug or Webkit Inspector.
// the return value of `evaluate` here is array of tests.
this.evaluate(testReporter).forEach(function(assertion){
// loop through the test, calling casper's `assert` method
// which will print to the console.
this.test.assert(assertion.didPass, assertion.text);
},this);
});
// run all the `then`s and call `test.renderResults` when they're done.
casper.run(function(){
this.test.renderResults(true);
})
@trek
Copy link
Author

trek commented Jul 22, 2012

I use this to run my unit tests as part of the same process as my larger integration tests, all via the command line. My script is a bit more featured than this, but this stub will get you started. If you use jasmine, qunit, or something else for unit tests, just figure our the DOM structure of the test reporter and use query selectors to turn them into objects.

@b00gizm
Copy link

b00gizm commented Jul 22, 2012

So you're basically parsing the output of the test reporter and you still had to run mocha in advance, right?

@trek
Copy link
Author

trek commented Jul 22, 2012

@b00gizm What do you mean by "run mocha in advance"?

@svperfecta
Copy link

Nice @trek. Thanks for this.

@mallim
Copy link

mallim commented Jun 18, 2013

Thanks for the gist which is very nice for checking the test results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment