Skip to content
A JavaScript mock framework
JavaScript Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



A mock framework for JavaScript in the spirit of RR.

To run the tests, build the dist/jj.js file by running rake dist.


Sample object:

var item = { foo: function() { return “FOO”; }, bar: function() { return “BAR”; } }; // => “FOO”; // => “BAR

Stubbing methods that have already been defined

If you just want to stub a method that’s already been defined for
the target object, you can use the most concise form of JJ stub
declaration, which is just calling returns on the stubbed method,
passing the value you’d like returned:

JJ.stub(item).foo.returns(“STUBBED”);; // => “STUBBED”; // => “BAR

Stubbing undefined methods

The syntax for stubbing methods that are not defined on the target
object is more verbose, though not by much. Call method on the stub
proxy, passing the name of the method you’d like to stub. Then call
returns on that.

Note that you can also use this style for stubbing methods are defined
on the target object. I just don’t think it’s as pretty.

JJ.stub(item).method(‘whiz’).returns(‘bang!’); item.whiz(); // => “bang!” JJ.reset(item); item.whiz(); // => TypeError

Resetting an object:

To reset an object, just call JJ.reset and pass the object.

JJ.reset(item);; // => “FOO”; // => “BAR

Stub Contexts

In an attempt to get the kind of dynamic stub declaration that RR
provides, I’m working on “stub contexts” for JJ, where you pass a
function as the second argument to the JJ.stub call. Within there,
you can stub methods that may or may not actually be implemented in
the target object:

JJ.stub(item, function(stub) {“FROM CONTEXT”); // foo is already defined stub.mix.returns(“A NEW METHOD”); // mix is added dynamically }); // => “FROM CONTEXT” item.mix() // => “A NEW METHOD” JJ.reset(item); // => “FOO” item.mix() // => TypeError

Note that this feature is still a bit buggy. Use with caution.

Big credit should go to Brian Takita for creating RR, the best
doggone’ test double framework there ever were.

Using with Screw.Unit

To use JJ with Screw.Unit, make sure you’re using Brian’s recent branch
that passes the screwContext into your tests. Then, in an after block,
add JJ.verify(screw):

Screw.Unit(function(screw) { with(screw) {
  describe("JJ", function() {
    var item;
    before(function() {
      item = { foo: "FOO!" };
    it("receives foo", function() {
    after(function() {

You can also set how many times the method should be called:


This will make your test only pass when the foo method gets
called 3 times on item. If you don’t specify a specific number,
then your test will pass as long as the method was called at least once.


  • Code cleanup. Big time.
  • Mocks
  • argument awareness
  • Probes
  • Integration with Screw.Unit
  • Spies

© Copyright 2008 Pat Nakajima, released under MIT License.

You can’t perform that action at this time.