Geeks With Blogs
Annie Bougie
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:
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
Requesting Gravatar...
hmm..interesting..thanks for sharing
Left by Grey on Jul 24, 2017 12:43 AM

# re: Javascript Testing
Requesting Gravatar...
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:
 (will show your gravatar)


Copyright © Annie Bougie | Powered by: GeeksWithBlogs.net