Skip to content

Instantly share code, notes, and snippets.

@netpoetica
Created September 20, 2012 00:52
Show Gist options
  • Save netpoetica/3753310 to your computer and use it in GitHub Desktop.
Save netpoetica/3753310 to your computer and use it in GitHub Desktop.
Unit Test functions for basic client-side unit testing (will add more as I use them in my code)
/*
Name: UnitTest.js
Desc: Various unit testing functions. I use these for running quick tests
especially against JavaScript functions and variables, which are loosely type
and typically can behave unexpectedly
Location: https://dl.dropbox.com/u/30820392/JS_Utils/UnitTest.js
JavaScript Style Guide - Readable, Consistent Code
Yahoo, Google, Mozilla, jQuery recommendations
Naming:
Variables and Functions
- Allowed letters, numbers, $, and _
- private properties use _ before var name
- use camelCase ;)
Objects
- uppercase first letter (Math, Date)
Brace Style: open curly brace on the same line as the keyword
if (x) {
//....
}
Functions - define them before you call them for readability
Google Closure Compiler (minifier, in browser)
http://closure-compiler.appspot.com/home
*/
// Setup namespace
var Test = Test || {};
// Get the return type of a function. This can be useful in testing to make sure
// a large function with lots of callbacks and data manipulation is returning
// the type you're expecting to work with on your next procedure. JavaScript can
// easily return any object, number, string at multiple points of the same function
Test.returnTypeIs = function(myFunction){
return typeof myFunction;
}
// Returns true if the function you test returns the type you expect ('number', 'string', 'object')
Test.returnsType = function(myFunction, expectedType){
if(typeof myFunction === expectedType){
return true;
}
else{
return false;
}
}
// Setup a basic function for testing
var a = (function(){
return 12;
})();
// Log some tests
console.log(Test.returnTypeIs(a));
console.log(Test.returnsType(a, "undefined"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment