Last active
August 29, 2015 14:13
-
-
Save tecoholic/937f51b6889448836db8 to your computer and use it in GitHub Desktop.
A Sample Snippet showing Unit Testing of JS Libraries using CasperJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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