Coder Social home page Coder Social logo

jack's Introduction

Introduction

Jack is a toolkit for mocking JavaScript objects and functions when writing tests. It is inspired by similar projects for Java and C#, like JMock, Mockito and Rhino Mocks

Mocking can be achieved quite easily in JavaScript without help from a library, but anything above verifying that a function gets called can grow hard to read. If you have experienced this (or want to avoid it in the first place), Jack is for you.

Contributions

Contributions are very welcome. Just fork git://github.com/keronsen/jack.git and get started.

The head developer for Jack is Karl-Erik Rønsen

Contributions have been received, accepted and appreciated from:

Getting started

Download the latest version of Jack from github: http://github.com/keronsen/jack
General examples are found below.
There are examples of use with different test frameworks in /docs/examples.

If you need help, there is a google group for discussion: http://groups.google.com/group/jack-discuss

Test framework integration

Jack is currently compatible with these JavaScript test frameworks:

Integration is planned for these frameworks:

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 for testing interaction

If you want to test how your code under test interacts with other objects, you can create a mock object with jack.create():

jack(function(){
   var myStack = jack.create("myStack", ['push','pop']);
   jack.expect("myStack.push")
      .exactly("1 time")
      .whereArgument(0).is("something");
   useTheStack(myStack);
});

jack's People

Contributors

keronsen avatar francois2metz avatar duncanbeevers avatar

Watchers

Katie Gengler avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.