Java Script mock frameworks comparison

In this short series I’ve already looked at xUnit and BDD style testing frameworks for JS. As mocking is a fundamental part of testing and only two of JS frameworks (Jasmine and JSpec) have mock frameworks built-in, now it’s turn to look at standalone mock frameworks.

Jasmine’s and JSpec’s built-in solutions will be also tested here to see how they compare to their stand-alone counterparts.

Two flavors of mock frameworks

There are two approaches to verifying expectations when using mock objects as spies:

  • setting expectations before tested code is run
    expect(Klass.method).toBeCalledWith('foo argument');
    Klass.method('foo argument');
  • running tested code first and specifying assertions afterwards
    spyOn(Klass, 'method');
    Klass.method('foo argument');
    expect(Klass.method).wasCalledWith('foo argument');

The difference between both methods is mostly cosmetic, although I prefer second one (assertions after the code is run) for two reasons:

  • it is more consistent with the standard flow of test code – you normally invoke tested class’ methods first and then use “assert” statements on their return values, not the other way round.
  • it makes moving parts of mock / class initialization code into “before_each” easier, thus allowing you to keep your test code more DRY.



Expectations after tested code.


  • Easy integration with many testing frameworks.
    JsMockito provides special API calls for flawless integration with ScrewUnit, JsTestDriver, JsUnitTest, YUITest, QUnit and JsUnity.
  • Ability to mock both object methods and functions.
  • Correctly handles expectations set on tested code using “call” and “apply”.
  • Robust input arguments matchers.


  • Almost none. The only con I have is that it depends on jsHamcrest for input parameter matching instead of being fully self-enclosed, but this is really very minor one.


A very solid framework. If your testing framework of choice doesn’t have built-in mock framework, JsMockito is definitely your best option.



Expectations first, before tested code.


  • Allows mocking native methods (e.g. window.alert).
  • Extends jqUnit with powerful set of additional assertions (that can be used for matching input parameters in mocked methods as well as standalone).


  • Works only with jqUnit.


JqMock is quite robust framework. However, it it designed especially for jqUnit (a wrapper for QUnit library, which, although quite popular, is rather far from being my framework of choice). So, if you use jqUnit, jqMock is a decent and quite obvious choice. However, if you don’t use jqUnit (just like me) you’re out of luck (and jqMock is not that good to justify switching to jqUnit especially for it).

Jasmine’s built-in mock framework


Expectations after tested code.


  • “callThrough” option.
    Ability to spy on a method (so number of calls, passed parameters etc. can be verified as usual) but passing the call to (and get the returned value from) the original method afterwards.
  • “callFake” option.
    Ability to specify a custom callback function to be invoked when the spy is called (awesome for e.g. mocking jQuery AJAX calls) .
  • Additional properties for spys (“callCount”, “mostRecentCall”, “argsForCall”) helpful for making assertions after mocked method was called.


  • Works only in Jasmine.


Definitely my mock framework of choice. Especially the “callFake” method is an icing on a tasty cake. The major drawback is that it is built into Jasmine – so if you use other framework you are unfortunately out of luck. But hey, as you can read in my BDD JS frameworks comparison, you have no good excuse to not use Jasmine in the first place.

JSpec’s built-in mock framework


Expectations first, before tested code.


  • Mocking AJAX requests.
  • Mocking timers (“setTimeout” and similar methods).
  • No need to explicitly call “verify” at the end of a test.


  • Works only in JSpec.


Quite solid framework with a couple of interesting features, although not so polished as Jasmine’s. If you’re using JSpec, there is no reason to replace it with other, external framework, but there’s also no reason to switch to JSpec just for it’s mocking capabilities.

Other Frameworks

  • JSMock (
    It’s mature (current version is 1.2.1) and the basic features expected from a mock framework are in, however the documentation is lacking (there are a few basic examples, but I couldn’t find API docs anywhere) and it is quite stale (the newest version is from May 2007) so it is not worth investing your time.
  • Amok (
    It seems quite interesting, with some nice features like ability to mock AJAX calls and decent documentation (although a bit lacking in the area of aforementioned AJAX mocking). However, current version is 0.3 and was not updated for almost 18 months. On the other hand, documentation was updated much more recently (about 3 months ago). So my final verdict it to avoid it for now, with option to check in future if this project is live again.
  • Jack (
    The latest version is 0.0.2-alpha and not updated for over a  year. Probably an abandoned project.
  • JJ (
    It is still a work in progress. Right now it is practically only a basic stubbing framework, missing fundamental features like mocks / spies. Not really usable yet.

The Final Verdict

To make it short: my favorite framework is Jasmine’s built-in one. It is as good and polished as the rest of this top-notch framework. If you are just selecting JS testing framework (or are able to switch) Jasmine is your best choice.

If you are using either jqUnit or JSpec and are not willing to switch, use jqMock or JSpec’s built-in one, respectively, as there is no strong reason to substitute them with an external solution.

If you’re using any other xUnit or BDD framework (especially JsTestDriver, YUITest or ScrewUnit) your top choice is, hands down, jsMockito.

About these ads


  1. Christian said

    Just stumbled upon your blog, very interesting stuff! I’d be very interested to hear your thoughts on my (relatively new) spy/stub/mock framework Sinon.JS: There’s also a series of blog posts detailing some design decisions and usage:

    Sinon.JS is standalone, but I’m working on adapters for several test frameworks to get automatic verification and cleanup with as little boiler-plate as possible.

  2. velesin said


    At a first glance your framework looks very nice! It packs a lot of cool functionality, especially robust XMLHttpRequest / server response mocking seems interesting. I definitely plan to give it a more thorough look soon!

  3. [...] [...]

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: