mini-mock
An ultra-lightweight mocking framework for Node
Why?
I got tired of the complexity of using mocking frameworks for JS. I wanted something really simple, that does
just what I want it to do.
General
- For Javascript version: ECMAScript 6
- Written with the 'single object under test' approach in mind
- Object under test should ideally use constructor injection (all dependencies injected via the constructor)
- Records call count on every stubbed function
- Stubs asynchronous and synchronous functions
- After mock instances are returned via the
create()
function, Mocker resets its internal references, so it can immediately be reused
Usage examples
Asynchronous mocks (ie: functions using callbacks)
Without callback args
var Mocker = require('mini-mock');
var mocker = new Mocker();
var foo = mocker.mock(Foo.prototype)
.withAsyncStub('forward', null)
.withAsyncStub('reverse', null)
.create();
var objTotest = new Bar(foo);
objToTest.go();
expect(foo.recorder['forward'].calls).to.equal(1);
expect(foo.recorder['reverse'].calls).to.equal(1);
With callback args
var Mocker = require('mini-mock');
var mocker = new Mocker();
var foo = mocker.mock(Foo.prototype)
.withAsyncStub('forward', [null, {key1: value1}])
.withAsyncStub('reverse', [null, {key2: value2}])
.create();
var objTotest = new Bar(foo);
objToTest.go();
expect(foo.recorder['forward'].calls).to.equal(1);
expect(foo.recorder['reverse'].calls).to.equal(1);
Synchronous mocks (ie: immediate return)
var Mocker = require('mini-mock');
var mocker = new Mocker();
var bar = mocker.mock(Bar.prototype)
.withSyncStub('go', foo)
.create();
var objTotest = new Robot(bar);
objToTest.walk();
expect(bar.recorder['go'].calls).to.equal(1);
See also the tests in /test
.