Skip to content

Instantly share code, notes, and snippets.

@tecoholic
Last active August 29, 2015 14:13
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 tecoholic/937f51b6889448836db8 to your computer and use it in GitHub Desktop.
Save tecoholic/937f51b6889448836db8 to your computer and use it in GitHub Desktop.
A Sample Snippet showing Unit Testing of JS Libraries using CasperJS
/*
Method 1: By loading the library through a web page.
The usability is pretty limited due to what can be returned through evaluate() is limited.
*/
casper.test.begin("Setting up test", 3, function suite(test){
casper.start("http://localhost/livetransit/test/", function(){
// Asserting pages would help
test.assertTitle("Test page", "page exists as expected");
});
casper.then(function(){
test.assertEvalEquals(function(){ return LiveTransit.getName(); }, "Live Transit", "Library Live Transit is loaded");
test.assertEvalEquals(function(){ return LiveTransit.name; }, "Live Transit", "Library name checked");
test.done();
});
casper.run(function(){
test.done();
});
});
/*
Method 2: Loading the JS file directly like a module using the require() function.
Here the tests are straight forward just as it is supposed to be for direct Unit Testing
*/
casper.test.begin('Loading LT library', 2, function(test){
var lt;
lt = require('../../src/livetransit.js');
test.assertEquals(lt.getName(), 'Live Transit');
test.assertEquals(lt.name, 'Live Transit');
test.done();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment