Geeks With Blogs
Welcome to
Geeks with Blogs
Login
Anne Bougie
23 Posts
| 86 Comments
My Other Recent Posts
Part 2: Copy a SharePoint List Programmatically
Another SharePoint Content Deployment Issue
SharePoint Content Deployment Issue
SharePoint Adventures - Reading an Excel Spreadsheet From A Stream
Part 1: Copy a SharePoint List Programmatically
InternalsVisibleTo with Strongly-Type Assemblies
Matching and Grouping Regular Expressions using Regex in C#
Recursive Search for a Single File
LINQ to SQL with ReSharper
News
Contact Me
anne.bougie@gmail.com
Post Categories
Non-Tech
.NET
Architecture
BI
BizTalk
Hardware
Information Worker
LINQ
SQL
TFS
Utilities
WCF (Indigo)
WinFx
SharePoint
MyMiniUrl
Archives
December 2016 (1)
September 2013 (1)
April 2013 (2)
April 2010 (1)
March 2010 (2)
August 2009 (1)
July 2009 (2)
March 2009 (3)
January 2009 (2)
October 2008 (1)
September 2008 (1)
August 2008 (1)
April 2008 (1)
Annie Bougie
<< Sucky MSTest and the "WaitAll for multiple handles on a STA thread is not supported" Error
|
Home
|
What is this, Javascript? >>
Javascript Testing
Comments (2)
|
Share
I've been working with Node.js projects for the last several months, and have been charged with creating a set of testing tools for automated unit testing. I've settled on Mocha.js, even though there are several others that are widely used, such as Jasmine. Mocha is really working well for sever side code without a lot of dependencies. The biggest issue that has been plaguing my efforts is dependencies and lack of mocking. Since server-side testing is the easier problem to solve, that's where I'm starting. I've decided to start with the following tools:
Test Runner: Mocha.js (
http://visionmedia.github.io/mocha/
)
Mocking Framework: Sinonjs (
http://sinonjs.org/
)
Here are some basics on how to use the above tools. I'm assuming you have Node and npm already set up on your machine. If you don't, go and set that up first:
http://joyent.com/blog/installing-node-and-npm
First, install Mocha:
npm install -g mocha
Create a small test file to make sure it's working. Call it my_test.js. I'm using Node's assert module.
var assert = require('assert');
var startsWith = function(input, startsWith) {
return input.indexOf(startsWith) == 0;
}
describe('sample mocha tests', function() {
it('should return -1 when the numeric value is not present in an array', function(){
assert.equal(-1, [1,2,3].indexOf(0));
});
it('should return true when string starts with the test value', function(){
var result = startsWith("Javascript Testing", "Java");
assert.ok(result);
});
it('should assign the hex character values to a string', function(){
var s = String.fromCharCode(0x003C, 0x003D);
assert.equal('<=', s);
});
});
Run the test; type "
mocha
" on the command line. You should get three tests passing.
Next, I'll use a slightly more complicated example to show how to use Sinon. Since I'm just learning Sinon, I borrowed an example from their site. Please check out their entire suite of excellent examples at sinon.org.
First, install the Sinon module:
npm install sinon
Save this into a file called "
clock.js
".
var sinon = require('sinon'),
assert = require('assert');
var clock;
function throttle(callback) {
var timer;
return function () {
clearTimeout(timer);
var args = [].slice.call(arguments);
timer = setTimeout(function () {
callback.apply(this, args);
}, 100);
};
}
before(function () { clock = sinon.useFakeTimers(); });
after(function () { clock.restore(); });
it("calls callback after 100ms", function () {
var callback = sinon.spy();
var throttled = throttle(callback);
throttled();
clock.tick(99);
assert.ok(callback.notCalled);
clock.tick(1);
assert.ok(callback.calledOnce);
assert.equal(new Date().getTime(), 100);
});
Sinon has many useful tools. They have Spies, which is how you set expectations on code, then run the code to see if the expectations have been performed. A common scenario is to test to see if the code sent an email without having to actually send an email. They also have Mocks and Stubs. Mocks are basically stubs combined with spies. Stubs will just return a specified value or behavior so the test can run as expected each time. This eliminates the need to save to an actual database, or to run against another module which may not yet exist in a project under development. These tools have been available for years in C#, Java, and other mature languages, and it's been hard for me to do javascript coding without this support.
I love being able to mock out time, which was one of my biggest frustrations with trying to do a roll-your-own implementation of javascript testing. Javascript is inherently mockable, so I thought it would be easy, but there are some real challenges. Private variables and methods, and doing dependency injection without having to modify the code are the biggest. Some of my issues are no doubt because of my lack of experience with Javascript.
I'm going to try to use Signon with Mocha to get the server-side code to be 100% testable. Take that statement with a grain of salt because we all know that 100% coverage is neither possible, nor is it always desirable. Remember, we want to test our own code, not the modules we're using or the Node framework itself. Mocha also has code coverage, which is another reason I chose it.
Posted on Monday, April 8, 2013 3:00 AM |
Back to top
Comments on this post: Javascript Testing
#
re: Javascript Testing
hmm..interesting..thanks for sharing
Left by
Grey
on Jul 24, 2017 12:43 AM
#
re: Javascript Testing
Glad you enjoyed the article! I remember when I first started out with JavaScript when was working for
Essay Writing Service Every Student Needs
and there were no debuggers. The key is, as always, to repeat the mantra: "Eventually, this will make sense. Eventually, this will make sense..." :)
Left by
Gloria
on Jul 24, 2017 12:45 AM
Your comment:
Title:
Name:
Email: (never displayed)
(will show your
gravatar
)
Comment:
Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
Verification:
Copyright © Annie Bougie | Powered by:
GeeksWithBlogs.net
Popular Posts on Geeks with Blogs
0
Code Monkey Projectiles - Index
Geeks With Blogs Content Categories
ASP.Net
SQL Server
Apple
Google
SharePoint
Windows
Visual Studio
Team Foundation Server
Agile
Office
Design Patterns
Web
Azure
Brand New Posts on Geeks with Blogs
0
How do you run multiple instances of Microsoft Teams?
Need Something to Do over the Break? How About 25 Free JavaScript Courses?
Benefits of Apple Cider Vinegar
Shaking down the Raspberry Pi 400
Blog is Moving