New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mini-mock

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mini-mock

An ultra-lightweight mocking framework for Node

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
43
increased by104.76%
Maintainers
1
Weekly downloads
 
Created
Source

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

// set up mock
var Mocker = require('mini-mock');
var mocker = new Mocker();

var foo = mocker.mock(Foo.prototype)
    // async function stubs
    .withAsyncStub('forward', null) // 1st arg: function name; 2nd arg: callback arguments (null in this case)
    .withAsyncStub('reverse', null)
    .create();

// object under test
var objTotest = new Bar(foo);
objToTest.go();

// assertions
expect(foo.recorder['forward'].calls).to.equal(1);
expect(foo.recorder['reverse'].calls).to.equal(1);

With callback args

// set up mock
var Mocker = require('mini-mock');
var mocker = new Mocker();

var foo = mocker.mock(Foo.prototype)
    // async function stubs
    .withAsyncStub('forward', [null, {key1: value1}]) // 1st arg: function name; 2nd arg: callback arguments (null error; object result)
    .withAsyncStub('reverse', [null, {key2: value2}])
    .create();

// object under test
var objTotest = new Bar(foo);
objToTest.go();

// assertions
expect(foo.recorder['forward'].calls).to.equal(1);
expect(foo.recorder['reverse'].calls).to.equal(1);

Synchronous mocks (ie: immediate return)


// set up mock
var Mocker = require('mini-mock');
var mocker = new Mocker();

var bar = mocker.mock(Bar.prototype)
    // sync function stubs
    .withSyncStub('go', foo) // 1st arg: function name; 2nd arg: expected result
    .create();

// object under test
var objTotest = new Robot(bar);
objToTest.walk();

// assertions
expect(bar.recorder['go'].calls).to.equal(1);

See also the tests in /test.

Keywords

FAQs

Package last updated on 19 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc