# jsmockito

> Javascript mocking framework inspired by the awesome mockito

Latest version **1.0.5** (published 2014-06-07) · 0 weekly downloads

## Install

```sh
npm install jsmockito
pnpm add jsmockito
yarn add jsmockito
bun add jsmockito
```

## Health

**Score 18/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2014-06-07 |
| First published | 2012-04-17 |
| Weekly downloads | 0 |
| TypeScript types | separate (@types/jsmockito) |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 106 |
| Author | Chris Leishman |
| Maintainers | robinduckett, stephenhand |

## Links

- npm: https://www.npmjs.com/package/jsmockito
- Repository: https://github.com/chrisleishman/jsmockito
- Issues: https://github.com/chrisleishman/jsmockito/issues
- npm.io page: https://npm.io/package/jsmockito

## Dependencies (1)

- [jshamcrest](https://npm.io/package/jshamcrest.md) 0.6.7

## Recent versions

- 1.0.5 (latest) — 2014-06-07
- 1.0.4 — 2012-04-17

## README

JsMockito
---------

  http://github.com/chrisleishman/jsmockito/tree/master

JsMockito is a JavaScript stub/mock framework heavily inspired by
Mockito. To quote the mockito website:

  "Mockito is a mocking framework that tastes really good. It
   lets you write beautiful tests with a clean & simple API.
   Mockito doesn't give you a hangover because the tests are
   very readable and they produce clean verification errors."

JsMockito aims to try and reproduce the clean & simple API, with a
JavaScript twist. And why not add some variation to your drinking habits?


What do you serve it with?
--------------------------

JsMockito must be served with JsHamcrest.  Not only do they go well
together, it's essential to avoid a very nasty hangover.  Get
JsHamcrest from here:

  http://github.com/danielfm/jshamcrest/


How to drink it?
----------------

To use JsMockito with a JavaScript unit test framework, follow the usual
installation/configuration instructions for the framework and plug JsMockito
into it. If you're integrating with Screw.Unit (and why wouldn't you?) then you
just need to make the following calls:

    JsHamcrest.Integration.screwunit();
    JsMockito.Integration.screwunit();

Once installed, you can verify with interactions:

    var mockedObject = mock(Array);
  
    // -- start code under test --
    mockedObject.push("one");
    // -- end code under test --
  
    verify(mockedObject).push("one");

Or you can stub method calls:

    var mockedObject = mock(Array);
  
    when(mockedObject).get(1).thenReturn("hello world");
    
    // -- start code under test --
    alert(mockedObject.get(1));
  
    // the following alerts 'true' as get(99) was not stubbed
    alert(typeof (mockedObject.get(99)) === 'undefined');
    // -- end code under test --

For a JavaScript twist, you can also mock functions:

    mockFunc = mockFunction();
    when(mockFunc)(anything()).then(function(arg) {
      return "foo " + arg;
    });
  
    // -- start code under test --
    mockFunc("bar");
    // -- end code under test --
  
    verify(mockFunc)(anything());

    // or if you want to verify the scope it was called with, use:
    verify(mockFunc).call(this, anything())

Mockitos are also good for spys
-------------------------------

Real super spies don't drink martinis - they go for mockitos. And just like
Mockito, JsMockito supports 'spying' (e.g. partial mocking) on real functions
and objects and verifing how they were interacted with.

An example with functions:

    realFunc = function(msg) { alert(msg) };
    mockFunc = spy(realFunc);

    // -- start code under test --
    // the following alerts 'hello world'
    mockFunc("hello world");
    // -- end code under test --

    verify(mockFunc)("hello world");

or with objects:

    realObj = new Array();
    mockedObj = spy(realObj);

    when(mockedObj).pop().thenReturn("bar");

    // -- start code under test --
    mockedObject.push("foo");
    alert(realObj.length); // alerts '1'

    alert(mockedObject.pop()); // alerts 'bar'
    alert(realObj.length); // still alerts '1'
    // -- end code under test --
 
    verify(mockedObject).push("foo");
    verify(mockedObject).pop();

For more examples and documentation, run 'ant doc' and then look in the doc directory.


Who is your bartender?
----------------------

This variation is served to you by Chris Leishman and friends.  Also a big
thank you to the mockito guys for the inspiration!

Also thanks to the JsHamcrest authors, who made this easy.

---
_Source: https://npm.io/package/jsmockito · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
