Jack
Introduction
Jack is a toolkit for mocking JavaScript objects and functions. The project aims to help developers write short and readable JavaScript tests.
Status
2009-03-18: Finished a large refactoring. Tagged as version-0.0.2-alpha. More information in this blog post.
2009-02-28: Some changes have been planned. Read the blog entry here for some more information. The version that has been available for the last couple of months is now tagged as version-0.0.1-alpha.
2008-10-17: The project caught Ajaxians attention: http://ajaxian.com/archives/jack-mocks
Initial release note: Although we are pretty confident about the API, we still consider this Alpha software. We reserve the right to change anything and everything at this point. Please bear this in mind if you choose to use it on your project.
Download
- jack.js: (version-0.0.1-alpha): http://boss.bekk.no/repos/projects/jack/tags/version-0.0.1-alpha/src/jack.js
- jack.js: (trunk): http://boss.bekk.no/repos/projects/jack/trunk/src/jack.js
- SVN repository: http://boss.bekk.no/repos/projects/jack
Contact us
Main developer: Karl-Erik Rønsen (karl-erik.ronsen@bekk.no)
If you have some feedback for us, please send an email or leave a comment on this page.
Feature summary
- Set expectations for number of calls and argument constraints
- Specify mock implementations for specific expectation scenarios
- Create mock objects with a list of stub functions
- Reports expectation failures to test frameworks. Currently integrated with JSSpec and Scriptaculous TestRunner
Examples
For these examples, the application under test has a function storeSomething() that we expect will call the jQuery.post() function.
Expect one call to function
jack(function(){
jack.expect("jQuery.post").exactly("1 time");
storeSomething();
});
Available call quantifiers: never(), once(), exactly(), atLeast(), atMost()
Expect argument values
jack(function(){
jack.expect("jQuery.post")
.exactly("1 time")
.withArguments("http://example.com/service");
storeSomething();
});
Set other argument constraints
jack(function(){
jack.expect("jQuery.post")
.exactly("1 time")
.whereArgument(0).isOneOf("/serviceOne","/serviceTwo");
storeSomething();
});
Available constraint methods: is(), isNot(), isOneOf(), isType(), matches(), hasProperty(), hasProperties()
Specifying a mock implementation of a function
jack(function(){
jack.expect("jQuery.post")
.exactly("1 time")
.mock(function() {
// your mock implementation
});
storeSomething();
});
Specifying a return value
jack(function(){
jack.expect("jQuery.post")
.exactly("1 time")
.returnValue("The value to return.");
storeSomething();
});
Creating a mock object to run expectations against
jack(function(){
var mockObject = jack.create("mockObject", ['functionOne','functionTwo']);
jack.expect("mockObject.functionOne")
.exactly("1 time")
.whereArgument(0).is("something");
mockObject.functionOne("something");
});