Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
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");
test.assertEvalEquals(function(){ return LiveTransit.getName(); }, "Live Transit", "Library Live Transit is loaded");
test.assertEvalEquals(function(){ return; }, "Live Transit", "Library name checked");
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(, 'Live Transit');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment