Skip to content

Instantly share code, notes, and snippets.

@tecoholic tecoholic/test.js
Last active Aug 29, 2015

Embed
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");
});
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
You can’t perform that action at this time.